Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C, pointers, basic problem

Name: Anonymous 2006-05-20 14:45

Here is my code, just learning more about pointers:
include<stdio.h>
main()
 {
int x, *ptr_x;
int y, *ptr_y;
int z, *ptr_z;
x=64;
y=128;
z=256;
puts("This is a program used to display information on pointers:\n");
printf("x=%d and the pointer is %p\ny=%d and the pointer is %p\nz=%d and the pointer is %p\n",x,y,z,ptr_x,ptr_y,ptr_z);
return 0;
 }

And the output:
This is a program used to display information on pointers:

x=64 and the pointer is 0x80
y=256 and the pointer is 0x8fe07604
z=0 and the pointer is 0x1

Name: Anonymous 2006-05-20 14:50

OK, it took a while before I found out where you asked the question, but I do know the answer:

You forgot to specify the return type for main.

Name: Anonymous 2006-05-20 14:51

>>2
:)
You are right I didnt ask a question ^^;

And thanks, hopefully that fixes it

Name: Anonymous 2006-05-20 14:51

and the problem is? why do people always post a bunch of statements, and then look for an answer to some 'question' they never asked?

i assume your 'problem' is the fact that the output looks wrong. it's not tho. the order of your variables in the printf string is 'x, x_ptr, y, y_prt, z, z_prt', but the order of the parameters in the printf is 'x, y, z, x_ptr, y_ptr, z_ptr'. obviously, the order of the parameters should match the order of their appearence in the string you're printing.

Name: Anonymous 2006-05-20 15:03

>>4
Thanks

Name: Anonymous 2006-05-20 18:29

Ummm

The pointers were uninitialized.  Sure, they have values, that just happens to be garbage that existed in memory when it began.

Pointers point to locations in memory, right?  I assume you want ptr_x to point to the memory location of variable x, likewise with ptr_y and y, and ptr_z with z.

To do this you do:

ptr_x = &x;
ptr_y = &y;
ptr_z = &z;

the unary & operator before a variable returns the reference (aka memory address) of said variable.

Now ptr_x, ptr_y, and ptr_z should equal the memory locations of variables x, y, and z.

You can "dereference" a pointer by putting a * before it.  So *x_ptr doesn't return x_ptr -- it returns the value of the variable that x_ptr points to (in this case, x).  So you can rewrite the corrected version (with the order that >>4 specified) of your printf statement with just the pointers and their dereference:

printf("x=%d and the pointer is %p\ny=%d and the pointer is %p\nz=%d and the pointer is %p\n", *x_ptr, x_ptr, *y_ptr, y_ptr, *z_ptr, z_ptr);

Likewise, this line:

*x_ptr = 65;

will set the variable x to 65.  But

x_ptr = 65;

will set the memory address (technically it's an offset, but let's not worry about that for now) of x_ptr to 65 and will probably yield unexpected results.  This is one way to introduce memory leaks.

Name: Anonymous 2009-11-21 21:17

>>6
a helpful post on /prog/, i never thought i would live to see the day

Name: Anonymous 2009-11-21 21:33

>>7
I'm sorry. Something about this post is blinding me.
http://www.wikihow.com/Use-English-Punctuation-Correctly

Name: Anonymous 2009-11-21 21:44

>>8
what makes you think my sentence has ended? i may continue it later

Name: sage 2009-11-21 22:01

What a pile.

You forgot the # for the #include directive.  But it wouldn't compile if you actually did that so I will just assume that it was cut off when you copy+pasted.

1. Main needs to return int
2. Learn to indent your god damn code
3. Use code tags
4. Stop bumping a 3 ½ year old thread

Name: Anonymous 2009-11-21 22:07

From a Jew's face
The wicked Devil speaks to us,
The Devil who, in every country,
Is known as an evil plague.

Would we from the Jew be free,
Again be cheeful and happy,
Then must youth fight with us
To get rid of the Jewish Devil.

Name: Anonymous 2009-11-22 4:19

1. Main needs to return int
It actually does, what do you think it is, sepples?

2. Learn to indent your god damn code
I guess he did it, however,
3. Use code tags
ruined all his work.

Name: Anonymous 2010-06-07 6:40

Hi, I can spam /prog/ too, you faggot.

Also, smoke weed everyday.

Name: ​​​​​​​​​​ 2010-10-23 0:54

Name: Anonymous 2011-08-02 19:41

>>12
That's exactly it

Name: Anonymous 2011-08-02 19:50

>>10
2. Learn to indent your god damn code

This is C, not FIOC. Only Phython faggots intent their code.

Name: Sgt.Kabu磦똅kiman뒖멷 2012-05-28 18:54

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List