help in pointers

This is a discussion on help in pointers within the C Programming forums, part of the General Programming Boards category; i have read many about pointers and understood very good. but still there are some confusing thinks. please help to ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    3

    help in pointers

    i have read many about pointers and understood very good. but still there are some confusing thinks. please help to understand.

    1) int a[];
    why *&a doesn't give the first value of array? instead it gives the address. isn't *&a and *a same?

    2) int a[n][m], (*p)[n];
    for (p = a[0]; p < a[n]; p++) in this case it gives warning "assignment from incompatible pointer type" but it compiles and gives the right answer. if you put address operator before a there will not be problem. why? aren't they same?

    thank you

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,290
    Quote Originally Posted by littletics View Post
    1) int a[];
    why *&a doesn't give the first value of array? instead it gives the address. isn't *&a and *a same?
    They are not the same. The underpinning of your misunderstanding is that you believe an array and a pointer are the same thing. They are not. It is just that, in some contexts, the compiler can convert the name of an array into a pointer (to the first element of the array). There are lots of contexts where the compiler is forbidden from doing that conversion.

    Let's say we've declared a as an actual array.
    Code:
       int a[10];
    &a is of type "pointer to array of ten int". So *&a is of type "array of 10 int". It represents the whole array (all ten elements of it.

    In a function (when a is passed as argument to the function) things are a little different. So, if you do this;
    Code:
    void func(int a[])
    {
       /* body */
    }
    a, in the body of the function, is actually of type "pointer to int" So &a is a "pointer to pointer to int" and *&a is of type "pointer to int" (and that pointer has the same value as a).

    Quote Originally Posted by littletics View Post
    2) int a[n][m], (*p)[n];
    for (p = a[0]; p < a[n]; p++) in this case it gives warning "assignment from incompatible pointer type" but it compiles and gives the right answer. if you put address operator before a there will not be problem. why? aren't they same?
    This is one of the many cases where a pointer and an array are actually distinctly different things.

    a[0] is of type "array of m ints". p is of type "pointer to an array of n ints". Those types are incompatible.

    &a[0] is of type "pointer to array of m ints". If m and n are equal, that is the same type as for p (which is presumably happening in your code, although you haven't shown it). If m and n are not equal, then &a[0] and p have different types - the compiler will generally complain, again, about incompatible pointer types.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    @grumpy, I didn't get your explanation. I too, assumed it would work. I mean
    Code:
     int a[10] = {10,3,0,} ; 
    
      printf("%p %p", a, &a[0]) ; //This would both give me the address of a 
      printf("%p",&a)  //This also would give me the address of a 
    
      printf("%d %d", *a, *&a[0]) //Would give me the number 10, rightfully
    
      printf("%d", *&a)// doesn't work, however
    Why does *&a not work? It still gives me the address of a.
    &a is of type "pointer to array of ten int".
    &a points to the first element in the array of ten ints, yeah?
    So *&a is of type "array of 10 int". It represents the whole array (all ten elements of it.
    I don't understand. What do you mean of type "array of 10 int"? So it is equivalent to saying :
    Code:
          int *ptr = &a ;
    You ended that sentence with a preposition...Bastard!

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    &a and a will give you same value(address) but the type is different.
    &a + 1 is equal to (char*)&a + sizeof(a). since &a type is pointer to array.
    a + 1 gives (char*) a + sizeof(a[0]).

    Try to print them out and see for yourself.
    Code:
    printf("a is at %p\n",a);
    printf("&a is %p\n",&a);
    printf("&a + 1 = %p\n",(&a) +  1 );
    printf("a + 1 = %p\n",a + 1 );
    Question 6.2

    isn't *&a and *a same?
    *& cancel each other..

    Code:
    int x;
      *&x = 3;       // same as x = 3!

  5. #5
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    oh yes, i see what grumpy means
    It also explains, why *&a[0] works. cool!
    You ended that sentence with a preposition...Bastard!

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    thank you guys, i have understood the type difference &a and a.
    i am sorry i have a mistake in the 2 question in declaration
    2) int a[n][m], (*p)[n];
    it should be (*p)[m]. after all, now i can answer my 2 question myself (please correct if wrong)

    Answer: however p is type of "pointer to array of m ints" and a[i] is type of "pointer to the "i"s rows first element of 2D array", they are incompatible. for compatibility it must be &a[i], which is type of "pointer to array of m ints" and represents the whole array of m elements of the "i"s row of 2D array

    and one more question, as i have read many about that, but never met the explanation of type difference &a and a. where did you know that?
    Last edited by littletics; 01-04-2011 at 01:06 PM.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,290
    The difference between &a and a is part of C.

    The basic rule is: If x is of type "thing", then &x is always of type "pointer to thing" and has a value called "address in memory of x". This rule is always true.

    The problem you've had with understanding is that there is special handling for arrays, so the compiler treats "a" and "&a[0]" as equivalent in some contexts. The "in some contexts" is underlined as it is important - arrays and pointers are different things so, sometimes, they can be treated as equivalent and sometimes they can't.



    In terms of how I learned that ..... come around the campfire, young brave.

    Many many moons ago I, like you, had read an introductory text that told me "a pointer and an array are the same thing".

    Eventually, I was writing code, and ran into a case where the compiler complained.

    I was arrogant, and insisted the compiler was broken because, after all, "a pointer and an array are the same thing".

    However, the stars and the moon were not content with my arrogance.

    I had to work with code that could be ported between different operating systems and compilers. All of the compilers complained about my code.

    Compilers are like the buffalo - they are wise, they do not change often, and they are unforgiving of your errors. No matter how I insisted that I was right, the compilers kept rejecting my code.

    I consulted with other buffaloes. Every one rejected my code.

    I consulted with a fellow brave, and he could not explain the way of the buffalo to me.

    Faced with the wisdom of multiple buffaloes, I was forced to humbly ask the heart-wrenching question "what am I doing wrong?".

    By asking that question, I took the a step towards enlightenment, and realised that the basic texts had taught me incorrectly.

    I then consulted with a chieftain - an experienced programmer - who told me "that's the way it is".

    I was unsatisfied with the guidance of that chieftain, so I went on a quest to find a wiser chieftain - a programmer who could explain the real world to me.

    Eventually, on my hands and knees, I found a chieftain who had new and mighty knowledge - he had buffalo skins branded with the text of the (then) new C standard. After abasing myself to the chieftain, he permitted me to examine the skins.

    I paid homage to those skins - and spent much time attempting to understand the strange markings.

    Such were my first steps on my never-ending path towards wisdom.....
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,488
    Quote Originally Posted by grumpy View Post
    Compilers are like the buffalo
    I find compilers to be more like a bison or yak.

  9. #9
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    correct.
    Code:
     
          int (*p)[2][10] ;
    Is a pointer to an array of 10 integer elements. Notice that it isn't an array of 10 pointers unlike
    Code:
          int *p[2][10]
    Since it is a pointer that has a width of 100, when you increment the pointer, it would skip 100columns.
    I will rather prefer this declaration when using strings.
    You ended that sentence with a preposition...Bastard!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays with pointers
    By Niels_M in forum C Programming
    Replies: 18
    Last Post: 07-31-2010, 03:31 AM
  2. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  3. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  4. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 06:28 AM
  5. Pointers pointers pointers....
    By G'n'R in forum C Programming
    Replies: 11
    Last Post: 11-02-2001, 01:02 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21