NO

Author Topic: Pointers  (Read 2719 times)

Mac1979

  • Guest
Pointers
« on: December 01, 2013, 12:00:18 AM »
I am new to c programming and am working on a code that uses pointers.
Code: [Select]
#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?
« Last Edit: December 01, 2013, 01:34:50 AM by Bitbeisser »

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Pointers
« Reply #1 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

dante80

  • Guest
Re: Pointers
« Reply #2 on: January 02, 2014, 09:53:59 PM »
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.