Thread: how to print pointer variable

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    18

    how to print pointer variable

    Hi all,

    Need explanation how can i print this :
    Code:
    int *pa = {1,2,3,4,5,6};
    if i use
    printf("%d",pa) ===> result is 1

    and if i use
    printf("%d",pa+1) ==> result is 5

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    printf("%p\n", (void*)mypointer);

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    18
    Quote Originally Posted by robwhit View Post
    printf("%p\n", (void*)mypointer);
    I follow your code, but i just got
    0x1

    I want to the whole array in variable can be printed, so the expectation result is
    1 2 3 4 5 6

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So print pa[0], pa[1], pa[2], ..., pa[5].

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    18
    Quote Originally Posted by tabstop View Post
    So print pa[0], pa[1], pa[2], ..., pa[5].
    still error ==> segmentation fault

    ok, i show the code:
    Code:
    int main()
    {
    	int *pa = {1,2,3,4,5,6}
    	for(j=0; j<5;j++)
    	{
    		printf(" pa = %d \n",pa[j]);
    	}
    }
    and error is
    Code:
    $ ./a.exe
          3 [main] a 2996 _cygtls::handle_exceptions: Error while dumping state (pro
    bably corrupted stack)
    Segmentation fault (core dumped)

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Good. That means that today we have learned that you cannot use array-initializer notation to initialize pointers. (The only exception is string literals; those are stored and have addresses. 1, 2, 3, 4, 5, 6 does not.) Didn't you wonder why your compiler said
    Code:
    temp.c: In function `main':
    temp.c:4: warning: initialization makes pointer from integer without a cast
    temp.c:4: warning: excess elements in scalar initializer
    temp.c:4: warning: (near initialization for `pa')
    temp.c:4: warning: excess elements in scalar initializer
    temp.c:4: warning: (near initialization for `pa')
    temp.c:4: warning: excess elements in scalar initializer
    temp.c:4: warning: (near initialization for `pa')
    temp.c:4: warning: excess elements in scalar initializer
    temp.c:4: warning: (near initialization for `pa')
    temp.c:4: warning: excess elements in scalar initializer
    temp.c:4: warning: (near initialization for `pa')

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    18
    Quote Originally Posted by tabstop View Post
    Good. That means that today we have learned that you cannot use array-initializer notation to initialize pointers. (The only exception is string literals; those are stored and have addresses. 1, 2, 3, 4, 5, 6 does not.) Didn't you wonder why your compiler said
    Code:
    temp.c: In function `main':
    temp.c:4: warning: initialization makes pointer from integer without a cast
    temp.c:4: warning: excess elements in scalar initializer
    temp.c:4: warning: (near initialization for `pa')
    temp.c:4: warning: excess elements in scalar initializer
    temp.c:4: warning: (near initialization for `pa')
    temp.c:4: warning: excess elements in scalar initializer
    temp.c:4: warning: (near initialization for `pa')
    temp.c:4: warning: excess elements in scalar initializer
    temp.c:4: warning: (near initialization for `pa')
    temp.c:4: warning: excess elements in scalar initializer
    temp.c:4: warning: (near initialization for `pa')
    Yes, there are a lot warning when compilation. I ask this because when i compare it with string that you have mentioned it.

    Usually i can use
    char *x = "hello";
    printf("%s \n",x) ==> hello
    printf("%c \n",x[4]) == o

    Thanks you, so i understand those values have no address

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    const char *x = "hello";
    printf("address of x is &#37;p\n", (void*)&x);
    printf("string pointed to by x is %s\n", x);
    printf("address of string pointed to by x is %p\n", (void*)x);
    printf("x[4] is %c\n", x[4]);
    printf("address of x[4] is %p = %p + sizeof(x) * 4\n", (void*)&x[4], (void*)x);
    Last edited by robwhit; 10-27-2008 at 08:20 AM. Reason: i don't need no stinkin' code tags... or const qualifiers...

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Use [code] tags robwhit you noob

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    When you assign string literals to char pointers, you should use const char pointers.
    http://cpwiki.sourceforge.net/Common...kes_and_errors
    And remember that in C, a lot of warnings are actually errors. Do not ignore them (a good idea is to actually make the compiler treat the warnings as errors, so it does not compile if there are any warnings). A good programmer fixes all warnings.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Perhaps you meant
    Code:
    int pa[] = {1,2,3,4,5,6};
    printf("pa = %d\n", *pa);
    printf("*(pa+1) = %d\n", *(pa+1));

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by itCbitC View Post
    Code:
    printf("*(pa+1) = &#37;d\n", *(pa+1));
    What is it with everyone and this stinking and horrible syntax?
    There is an array syntax for a reason. Use it.
    Code:
    printf("pa[1] = %d\n", pa[1]);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  3. returning a pointer to a destroyed variable?
    By krygen in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2004, 02:22 PM
  4. eh, help?
    By The_Nymph in forum C Programming
    Replies: 2
    Last Post: 04-25-2002, 02:27 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM