Self declared array problem.

Discussion in 'Mac Programming' started by jeremyn9, Jan 9, 2009.

  1. jeremyn9 macrumors newbie

    Joined:
    Jan 5, 2009
    #1
    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
     
  2. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #2
    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.
     
  3. ghayenga macrumors regular

    Joined:
    Jun 18, 2008
    #3
    So they easy way is:


    double globalArray[];

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

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    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.
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    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
     
  6. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    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.
     
  7. jeremyn9 thread starter macrumors newbie

    Joined:
    Jan 5, 2009
    #7
    Hi Ghayenga.

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

    Thanks for suggesting thou
     
  8. jeremyn9 thread starter macrumors newbie

    Joined:
    Jan 5, 2009
    #8
    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.
     
  9. jeremyn9 thread starter macrumors newbie

    Joined:
    Jan 5, 2009
    #9
    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?
     
  10. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #10
    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.
     
  11. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #11
    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.
     
  12. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #12
    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.
     
  13. jeremyn9 thread starter macrumors newbie

    Joined:
    Jan 5, 2009
    #13
    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 :)
     

Share This Page