Yahoo Clever wird am 4. Mai 2021 (Eastern Time, Zeitzone US-Ostküste) eingestellt. Ab dem 20. April 2021 (Eastern Time) ist die Website von Yahoo Clever nur noch im reinen Lesemodus verfügbar. Andere Yahoo Produkte oder Dienste oder Ihr Yahoo Account sind von diesen Änderungen nicht betroffen. Auf dieser Hilfeseite finden Sie weitere Informationen zur Einstellung von Yahoo Clever und dazu, wie Sie Ihre Daten herunterladen.

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 Antworten

Relevanz
  • vor 1 Jahrzehnt
    Beste Antwort

    *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

  • vor 1 Jahrzehnt

    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.

  • vor 1 Jahrzehnt

    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.

Haben Sie noch Fragen? Jetzt beantworten lassen.