Will I ever understand array pointers?

Discussion in 'Mac Programming' started by farmerdoug, Feb 8, 2010.

  1. farmerdoug macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    Code:
    float *y, *ys;
    int j;
    if ((ys = (float *)calloc(points*points,sizeof(float))) == NULL)
    		printf("ys\n");
    if ((ys1 = (float *)calloc(points,sizeof(float))) == NULL)
    		printf("ys1\n");
    ...
    memcpy[ys[i*points],ys1,j*sizeof(float)];  // line  220
    
    2dspline.c:220: error: subscripted value is neither array nor pointer

    At least I remembered (evidentually) to declare and calloc ys as a *float not **float.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Your memcpy is using [] instead of () to surround the arguments to the function.

    Also you declare y and ys but then use ys and ys1.
     
  3. ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #3
    ys[i*points] indexes ys at i*points, since ys is a pointer to floats that would be a float.

    You can't memcpy to a float.

    That explains the error.

    Now, for what you actually want to do: I have no clue.

    -edit-

    Oh, or what Mr. Duncan said there.
     
  4. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #4
    pointers

    thanks, I don' t think there is enough coffee in the world to wake me up. Also, the variables are defined somewhere. At least I got that right.
     
  5. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #5
    Chaos

    That might have something to do with it. There are work around, like loops for assigning things one at a time. Also assigning pointers
    p[0] = *y[0] or something similar.
     
  6. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    memcpy documentation says:

    Code:
    void *memcpy(void *s1, const void *s2, size_t n);
    
    The memcpy() function copies n bytes from the object pointed to by s2 into the object pointed to by s1.

    So if s1 is a float rather than a pointer then it won't work.
     
  8. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #8
    this compiled

    Code:
    float **ys, *ys1;
    
    
    if ((ys = (float *)calloc(points,sizeof(float))) == NULL)
    		printf("ys\n");
    		
    if ((ys1 = (float *)calloc(points,sizeof(float))) == NULL)
    		printf("ys1\n");
    
    	for (i = 0; i < points; i ++)
    		{
    	
    		if ((ys[i] = (float *)calloc(points,sizeof(float))) == NULL)
    			printf("ys[i]\n");
    		}
    
    	 memcpy(ys[i], ys1, j*sizeof(float));
    
    [code]
    
    I used the **ys, not to make memcpy work, but as required by Numerical Recipies in C.
     
  9. ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #9
    That copies the content of what y[0] is pointing to to p[0], This only works if y is a pointer to an array of pointers.

    When I say 'works' I mean that it does what you mean it to do, it might still compile otherwise but it most certainly won't do what you expect :)
     
  10. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #11
    farmerdoug, I'd also recommend you take the time to look over this web page covering "Macro Pitfalls"
     

Share This Page