Pelles C forum

C language => Beginner questions => Topic started by: Mac1979 on December 01, 2013, 12:00:18 AM

Title: Pointers
Post by: Mac1979 on December 01, 2013, 12:00:18 AM
I am new to c programming and am working on a code that uses pointers.
#include <stdio.h>

int main(void) {
int x, y;
int *p_int; /* Pointer to an int */

x = 4;
y = 0;
p_int = &x; /* Pointer has address of x ("points to x") */

printf("x=%d, y=%d, *p_int=%d, p_int=%X\n", x, y, *p_int, p_int);

y = *p_int; /* Y now contains 4 also */
p_int = &y; /* Pointer now has address of y ("points to y") */

printf("x=%d, y=%d, *p_int=%d, p_int=%X\n", x, y, *p_int, p_int);

*p_int = *p_int * 2;  /* Multiply what p points to by 2 */
printf("x=%d, y=%d, *p_int=%d, p_int=%X\n", x, y, *p_int, p_int);

return(0);
}

When I try to build it I am coming up with errors. I'm not sure where the errors are though. Can anyone help?
Title: Re: Pointers
Post by: Bitbeisser on December 01, 2013, 01:42:06 AM
Well, it always helps if you tell us which error you get that you don't understand...  ;)

In this case, the error is pretty obvious/self explanatory, just break the printf() statement down into printing each value separately and it will become apparent...  8)

Ralf
Title: Re: Pointers
Post by: dante80 on January 02, 2014, 09:53:59 PM
Quote from: Bitbeisser on December 01, 2013, 01:42:06 AM
Well, it always helps if you tell us which error you get that you don't understand...  ;)

In this case, the error is pretty obvious/self explanatory, just break the printf() statement down into printing each value separately and it will become apparent...  8)

Ralf

Thank you for posting this.  I was having the same problems.