returning an array

I am trying to return array by refrence. Seems like i did a mistake somewhere. could you please correct me?
1
2
3
float *pointer;
  pointer = returnarray;
  return pointer;


The problem is when I am getting it
1
2
float arrayc[2];
    (arrayc[0], arrayc[1]) = *computepoints(n);


thank you in advance
Last edited on
Many things going on. More code please.
Last edited on
closed account (zb0S216C)
You cannot return an array, only a pointer to a dynamically allocated region of memory. Some compilers may allow it, but a standard-compliant one won't.

Simple as.

Wazzak
but cant you return it was a refrence? I have done that pretty sure thats allowed
closed account (zb0S216C)
Yes you can (depends which compiler you use. Microsoft's compiler wouldn't allow it, the last time I checked), but why would you? Returning a private array? That'll break encapsulation (unless you declare the method constant). Returning a global or local array? Just access it directly.

The implementation looks like this: <TYPE>(&<FUNC_ID>(<PARAMETERS>))[<ARR_SIZE>];

Wazzak
Last edited on
well i need to return two values from A function. So I am putting them in a array and returning by refrence. I use mingw
closed account (zb0S216C)
No! Don't do that! Returning local variables isn't a good idea. In fact, it's heavily discouraged. Returning an array (not a reference to one, but a local one) is illegal. You'll have to dynamically allocate the array and return a pointer to it, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int *ReturnArray(const unsigned int);

int main()
{
    int *MyArray(ReturnArray(3));
    
    delete [] MyArray;
    return(0);
}

int *ReturnArray(const unsigned int Len)
{
    return(new int[Len]);
}


Wazzak
closed account (DSLq5Di1)
^ That is an ugly solution. If the values are related, why not use a structure?
closed account (zb0S216C)
@Sloppy9: It's an ugly world. The things is, we don't know what types of value he/she is returning. If the structure is large, DMA is inevitable.

Wazzak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
float *computepoints(int nameofcircle)
{
    float returnarray[2];
   if (nameofcircle ==1)
   {
       returnarray[0] = -4.5;
       returnarray[1] = 3.5;
    }
   else if (nameofcircle ==2)
   {
       returnarray[0] = -3.5;
       returnarray[1] = 3.5;

   }
   else if (nameofcircle ==3)
   {
       returnarray[0] = -2.5;
       returnarray[1] = 3.5;

   }
   else if (nameofcircle ==4)
   {
       returnarray[0] = -1.5;
       returnarray[1] = 3.5;

   }
   else if (nameofcircle ==5)
   {
       returnarray[0] = -4.5;
       returnarray[1] = 2.8;

   }
   else if (nameofcircle ==6)
   {
       returnarray[0] = -3.5;
       returnarray[1] = 2.8;
   }
   else if (nameofcircle ==7)
   {
       returnarray[0] = -2.5;
       returnarray[1] = 2.8;

   }
   else if (nameofcircle ==8)
   {
       returnarray[0] = -1.5;
       returnarray[1] = 2.8;

   }
   else if (nameofcircle ==9)
   {
       returnarray[0] = -4.5;
       returnarray[1] = 2.1;

   }
   else if (nameofcircle ==10)
   {
       returnarray[0] = -3.5;
       returnarray[1] = 2.1;

   }
   else if (nameofcircle ==11)
   {
       returnarray[0] = -2.5;
       returnarray[1] = 2.1;

   }
   else if (nameofcircle ==12)
   {
       returnarray[0] = -1.5;
       returnarray[1] = 2.1;

   }
   else if (nameofcircle ==13)
   {
       returnarray[0] = -4.5;
       returnarray[1] = 1.4;

   }
   else if (nameofcircle ==14)
   {
       returnarray[0] = -3.5;
       returnarray[1] = 1.4;

   }
   else if (nameofcircle ==15)
   {
       returnarray[0] = -2.5;
       returnarray[1] = 1.4;
   }
   else if (nameofcircle ==16)
   {
       returnarray[0] = -1.5;
      returnarray[1] = 1.4;

   }
   else if (nameofcircle ==17)
   {
       returnarray[0] = -4.5;
       returnarray[1] = 0.7;

   }
   else if (nameofcircle ==18)
   {
       returnarray[0] = -3.5;
       returnarray[1] = 0.7;

   }
   else if (nameofcircle ==19)
   {
       returnarray[0] = -2.5;
       returnarray[1] = 0.7;

   }
   else if (nameofcircle ==20)
   {
       returnarray[0] = -1.5;
       returnarray[1] = 0.7;

   }
   else if (nameofcircle ==21)
   {
       returnarray[0] = -4.5;
       returnarray[1] = 0;

   }
   else if (nameofcircle ==22)
   {
       returnarray[0] = -3.5;
       returnarray[1] = 0;

   }
   else if (nameofcircle ==23)
   {
       returnarray[0] = -2.5;
       returnarray[1] = 0;

   }
   else if (nameofcircle ==24)
   {
       returnarray[0] = -1.5;
       returnarray[1] = 0;

   }
   else if (nameofcircle ==25)
   {
       returnarray[0] = -4.5;
       returnarray[1] = -0.7;

   }
   else if (nameofcircle ==26)
   {
       returnarray[0] = -3.5;
        returnarray[1] = -0.7;

   }
   else if (nameofcircle ==27)
   {
       returnarray[0] = -2.5;
        returnarray[1] = -0.7;

   }
   else if (nameofcircle ==28)
   {
       returnarray[0] = -1.5;
        returnarray[1] = -0.7;

   }
   else if (nameofcircle ==29)
   {
       returnarray[0] = -4.5;
       returnarray[1] = -1.4;

   }
   else if (nameofcircle ==30)
   {
       returnarray[0] = -3.5;
       returnarray[1] = -1.4;

   }
   else if (nameofcircle ==31)
   {
       returnarray[0] = -2.5;
       returnarray[1] = -1.4;

   }
   else if (nameofcircle ==32)
   {
       returnarray[0] = -1.5;
       returnarray[1] = -1.4;

   }
   else if (nameofcircle ==33)
   {
       returnarray[0] = -4.5;
       returnarray[1] = -2.1;

   }
   else if (nameofcircle ==34)
   {
       returnarray[0] = -3.5;
       returnarray[1] = -2.1;

   }
   else if (nameofcircle ==35)
   {
       returnarray[0] = -2.5;
       returnarray[1] = -2.1;

   }
   else if (nameofcircle ==36)
   {
       returnarray[0] = -1.5;
       returnarray[1] = -2.1;

   }
   else if (nameofcircle ==37)
   {
       returnarray[0] = -4.5;
       returnarray[1] = -2.8;

   }
   else if (nameofcircle ==38)
   {
       returnarray[0] = -3.5;
       returnarray[1] = -2.8;

   }
   else if (nameofcircle ==39)
   {
       returnarray[0] = -2.5;
       returnarray[1] = -2.8;

   }
   else if (nameofcircle ==40)
   {
       returnarray[0] = -1.5;
       returnarray[1] = -2.8;

   }
   float *pointer;
   pointer = returnarray;
   return pointer;
}


there. I am returning that type of variables. Dont say why those if statements. It is complicated.
I am trying to get the x and y co-ordinates of where the drawing should be made when I get the input from the user.
closed account (DSLq5Di1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct point_t
{
    float x, y;
};

point_t computepoints(int nameofcircle)
{
    point_t pt;

   if (nameofcircle ==1)
   {
       pt.x = -4.5;
       pt.y = 3.5;
    }
    ...

    return pt;
}

point_t a = computepoints(n);
I returned it. However, there is problem on this line
1
2
point_t cor;
 cor = computepoints( nameofcircle);

well thanks I figured out my problem.
thank you very much all of you
Topic archived. No new replies allowed.