Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jeremyn9

macrumors newbie
Original poster
Jan 5, 2009
10
0
Hi

I am trying to initilaise an array manually, ie:

array[]={1,2,3,4,5}; using a function in this class called specialist. I have declared the array in the @interface section but whenever i call

-(void)inputprice
{
array[]={1,2,3,4,5};
};

to input array parameter, i get an error that says parse error before '{' token.
this can be solved by declaring the array as

-(void)inputprice
{
double array[]={1,2,3,4,5};
};

however tt presents a problem whereby the array is localised in the function only. I require it to be addressed elsewhere in the class.

any solutions?

thanks and regards
jeremy
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
Arrays can only be initialized like you are trying to do only when they are being declared, as you have observed when you put double in front of it.
Otherwise, you get to initialize it an element at a time.
 

ghayenga

macrumors regular
Jun 18, 2008
190
0
Arrays can only be initialized like you are trying to do only when they are being declared, as you have observed when you put double in front of it.
Otherwise, you get to initialize it an element at a time.

So they easy way is:


double globalArray[];

-(void)inputprice
{
double array[]={1,2,3,4,5};
globalArray = array;
};
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
Hi

I am trying to initilaise an array manually, ie:

array[]={1,2,3,4,5}; using a function in this class called specialist. I have declared the array in the @interface section but whenever i call

-(void)inputprice
{
array[]={1,2,3,4,5};
};

to input array parameter, i get an error that says parse error before '{' token.
this can be solved by declaring the array as

-(void)inputprice
{
double array[]={1,2,3,4,5};
};

however tt presents a problem whereby the array is localised in the function only. I require it to be addressed elsewhere in the class.

any solutions?

thanks and regards
jeremy

So I take it that you declared "array" as a member of the class? Like "double array [5];" between the { and }?

You'd have to write "array [0] = ...; array [1] = ...; " etc.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
So they easy way is:


double globalArray[];

-(void)inputprice
{
double array[]={1,2,3,4,5};
globalArray = array;
};

I was thinking this, too, but i don't know that it's safe. I think the behavior of this will be implementation specific. If the array literal was placed on the stack when inputprice was called, then once it gets popped that memory may be used for something else, resulting in globalArray pointing to garbage. I may be wrong about this, the literal may end up on the heap, sitting there valid forever, but like I said this may be implementation specific.

-Lee
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
I was thinking this, too, but i don't know that it's safe. I think the behavior of this will be implementation specific. If the array literal was placed on the stack when inputprice was called, then once it gets popped that memory may be used for something else, resulting in globalArray pointing to garbage. I may be wrong about this, the literal may end up on the heap, sitting there valid forever, but like I said this may be implementation specific.

-Lee

The compiler won't let you assign one array to another array anyway. You can assign an array to a pointer; that assignment will just assign a pointer to the first array element to the pointer variable. And there is a tricky bit with parameters: If you have a function

void f (int paramarray []) { ... }

it may look as if it is an array, but it is actually a pointer. In that case, a statement that looks as if it assigned an array to paramarray will compile, but it will be just a plain pointer assignment and have no other effect at all.
 

jeremyn9

macrumors newbie
Original poster
Jan 5, 2009
10
0
So they easy way is:


double globalArray[];

-(void)inputprice
{
double array[]={1,2,3,4,5};
globalArray = array;
};

Hi Ghayenga.

Ur method does not work. the compiler returns invalid use of flexible array member for globalArray = array;

Thanks for suggesting thou
 

jeremyn9

macrumors newbie
Original poster
Jan 5, 2009
10
0
So I take it that you declared "array" as a member of the class? Like "double array [5];" between the { and }?

You'd have to write "array [0] = ...; array [1] = ...; " etc.

Firstly yes array[] is a member of the class. Im having trouble addressing it in a function in the same class where i want to input values, eg array[]={1,2,3,4,5}; I can only do it locally in the function, i cant change the values of the class member array.

Well im trying not to do that as the array is v long(10000+ figures), therefore not feasible to. also its not fixed. so when i run for different cases, it maybe 11000 figures. Tts the reason for declaring the array without an exact number, ie array[].

A few post below suggest pointers. Can i use first declare the array locally then use a pointer to declare it globally? I dunno how to do this thou. any suggestions.
 

jeremyn9

macrumors newbie
Original poster
Jan 5, 2009
10
0
Problem solved partially

Guys

i solved the problem partially:

I initialised the array locally then called the class array to assign values from the local array to. In short i allocated array by array.

for (i=0;i<10000;i++)
{parray=tempparray;
}

This unfortunately poses another problem. The program crashed when i runned the loop above, but when i assigned individually: parray[2]=tempparray[2], it works exactly how i need it to.

Im presuming its a memory problem. Any way around it?
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
Your program could have crashed for several reasons - perhaps you don't really have 10000 elements, or, perhaps you've run out of stack space. You could allocate parray[10000] from the heap to get around the latter.
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
...Can i use first declare the array locally then use a pointer to declare it globally? I dunno how to do this thou. any suggestions.

Probably not. Since parray[] would be local to a function, when that function exited, it's stack space would be freed, and then your global pointer you set up from it would be worthless. You could, however, malloc the space for the local array, and then set your global pointer to it, and at program termination, free from your global pointer. That would be a perfectly acceptable solution.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
Firstly yes array[] is a member of the class. Im having trouble addressing it in a function in the same class where i want to input values, eg array[]={1,2,3,4,5}; I can only do it locally in the function, i cant change the values of the class member array.

Well im trying not to do that as the array is v long(10000+ figures), therefore not feasible to. also its not fixed. so when i run for different cases, it maybe 11000 figures. Tts the reason for declaring the array without an exact number, ie array[].

A few post below suggest pointers. Can i use first declare the array locally then use a pointer to declare it globally? I dunno how to do this thou. any suggestions.

Declaring an array without fixed size is an extension to the C and Objective-C language. To use this feature in a C struct you have to really know what you are doing (which, to be honest, I think is not the case right now). To use this feature in an Objective-C class, you really really have to know what you are doing; it's one of those things where if you ask how to do it, then the answer is "don't".

Use a pointer, and read up on how malloc (), realloc () and free () work. I think it would be helpful if you bought a good book about the C language as well. If you try to learn Objective-C without knowing C, there will be situations where you are just whistling in the dark.
 

jeremyn9

macrumors newbie
Original poster
Jan 5, 2009
10
0
Thanks everyone!!

I decided to go the long and dumb way:D:
i used excel to concentate the below line and cut and paste into the code.
works fine.


parray[0]=16.11;
parray[1]=16.12;
parray[2]=15.77;
parray[3]=16.19;
parray[4]=15.66;
parray[5]=17.06;
parray[6]=17.58;
parray[7]=16.63;
parray[8]=16.79;
parray[9]=16.73;
parray[10]=17.66;
parray[11]=17.44;
parray[12]=18.52;
parray[13]=17.51;.......

Once again thanks all for inputs :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.