#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Problem with Pointers


    #include <stdio.h>
    #include <conio.h>

    int main()
    {
    int a[]={2,5,6,8,10};
    int *p = a;
    printf("%d",a);
    printf("%d",*p);
    getch();
    }



    why the output executes only one printf statement?? :rolleyes:
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,158
    Rep Power
    2222
    What output do you get?

    What output do you get when you add a newline to those printfs? As in "%d\n".

    Also, use "%p" to print out a memory address.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0
    Oh, actually i didn't wrote new line \n
    that's why both the printf statements got mixed!

    Though in the above program is there any difference b/w these:
    p = a;
    *p = a;

    since both of them gives the same results..
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,158
    Rep Power
    2222
    I figured that, since you did nothing to visually separate the two outputs, you saw the two concatenated together and thought it was just one number. Though you should have been suspicious since the number was so large.

    Also, you ignored your compiler's warnings (assuming that you even have warnings turned on). Here is what I got (MinGW gcc on Win7):
    C:\otros\dcw>gcc -Wall newb2.c
    newb2.c: In function `main':
    newb2.c:8: warning: int format, pointer arg (arg 2)
    newb2.c:11: warning: control reaches end of non-void function

    C:\otros\dcw>
    That warning for Line 11 ("control reaches end of non-void function") is because you left out the return statement at the end of main(). You are quite correct to have main return int, but then you must also actually return an int. The convention is that if the program reaches the end successfully, then you return a zero. There are other programs and scripts in your computer's system that may need that information, so do get into the habit.

    The warning on Line 8 ("int format, pointer arg (arg 2)") is the important one here. This is the line that produced it:
    Code:
        printf("%d",a);
    The reason for that warning is that you tell printf to print a decimal integer (with the "%d"), but you don't give it an integer to display, but rather a memory address (AKA "pointer", a concept with which you will become intimately familiar in C). The compiler is warning us that that argument you gave it, a, is a pointer.

    Here is the output I got when I ran your program, which I altered ever so slightly by putting a space before the second number:
    C:\otros\dcw>a
    2686760 2
    C:\otros\dcw>
    Do you see that large number? Didn't you wonder what that was about? Well that is the "value" of a, and since the "value" of the name of an array is a pointer -- ie, the address in memory where that array starts -- that address gets converted to its integer equivalent and gets displayed.

    As I already told you, pointer values are supposed to be displayed using a different descriptor flag, "%p". That will display the address in hexadecimal, AKA "base 16", a very convenient way to display a binary value, far easier to work with than octal (base 8). And, the compiler won't throw warnings at you since now you are giving it the kind of value that you told it you would give it. Always tell the compiler the truth and always keep your promises to it.

    Here is your program with those corrections in it. Please note my use of code tags, which are needed to preserve your code's formatting:
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
        int a[]={2,5,6,8,10};
        int *p = a;
    
        printf("%p",a);
        printf(" %d",*p);
    
        getch();
        return 0;
    }
    And the output is:
    C:\otros\dcw>a
    0028FF28 2
    C:\otros\dcw>
    Originally Posted by alpha090
    Though in the above program is there any difference b/w these:
    p = a;
    *p = a;

    since both of them gives the same results..
    Quite clearly, they do not give the same results, nor do they even do the same thing.

    p is a pointer to int. As an array of int, a is also a pointer to int. Therefore,
    p = a;
    sets p to point to the array, a. This is a valid and quite normal operation.

    *p=a; is not normal nor is it valid, though the compiler, thinking that you are trying to do something clever, will try to do it anyway, but will throw a warning at you (which I got when I inserted that statement into your program):
    newb2.c:9: warning: assignment makes integer from pointer without a cast
    *p is what's called dereferencing. p points to a location in memory, while *p gives you access to the int value that is stored at that memory address. So then what that statement is trying to do is to store a pointer in an int. That is not a valid thing to do.

    But you don't do that in your program. Instead, you had this statement, which is quite different:
    Code:
        int *p = a;
    That statement declares p to be a pointer to int and it initializes it to point to the array, a. Initializing a variable when you declare it is different from assigning a value to a variable later on in the program.

    Comments on this post

    • alpha090 agrees : Absolutely Correct!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Smile


    Thanks a ton for explaining it thoroughly, Now It's Clear.

    I am using devcpp on Win 7 (I think it also uses the same compiler MinGW gcc).

IMN logo majestic logo threadwatch logo seochat tools logo