Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

pointers leaving me clueless, odd output, C?

so i'm screwing around just to get a feel for pointers, and also to familiarize myself with malloc() and friends.

anyways here is my little program:

#include <stdio.h>

int in;

int *p1 = ∈

int main(void)

{

printf("enter decimal: ");

scanf("%d", p1);

printf("%d", p1);

return 0;

}

/*what ends up happening is i get random numbers 7 digits long...are they the addresses of memory in decimal? i know if i change the last printf to %p i just get the address of memory that the pointer points to, in hex. how can i get it to use the pointer to print out the decimal that was taken in?*/

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    *p1 will dereference your pointer after you declared it.

    One of the reasons newcomers are confused by pointers, is the asterisk having kind of a double-meaning.

    when you declare a pointer

    its:

    int * p1;

    then call that variable by:

    p1; //Address

    to dereference it:

    *p1; //Value at the address of p1

  • 1 decade ago

    scanf("%d", *p1);

    printf("%d", *p1);

    you want the contents of p1(*p1) not p1

    note; Yep, my bad, I had hit submt when irelized you didn't get an error message. scanf wants a pointer.

  • 1 decade ago

    Either of these will work:

    printf("%d", in);

    printf("%d", *p1);

    Don't change your scanf as suggested by another....it's right the way it is.

Still have questions? Get your answers by asking now.