What is the Correct Way to Use Pointers in C?

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Value
AI Thread Summary
The discussion centers around understanding pointers in C, specifically how to correctly declare and use them. It clarifies that when a pointer is created, such as with "int *foo_ptr = &foo;", it points to the address of the variable "foo". Modifying the value at that address with "*foo_ptr = 42;" changes the value of "foo" to 42, not its address. The conversation also touches on the distinction between stack and heap memory, explaining that stack variables are temporary and tied to function scope, while heap variables persist until explicitly deleted. Additionally, the thread discusses function pointers, highlighting their utility in passing functions as arguments to other functions, such as in sorting algorithms. The importance of proper syntax and understanding variable declarations is emphasized, particularly the nuances of pointer initialization and the implications of declaring multiple variables on the same line.
transgalactic
Messages
1,386
Reaction score
0
when i read about pointers i got the idea that when i want to do a pointer to foo
int foo=35;

i do

Code:
*foo_ptr=foo;  //*foo_ptr returns the value 35
&foo_ptr         //has the address of foo

i have a problem in understanding this code
Code:
int foo;                         //1st line
int *foo_ptr = &foo;        //2nd line

int bar = *foo_ptr;         //3rd line

*foo_ptr = 42                //4th line

&foo is the address of foo not its value

*foo_ptr is a pointer to &foo which is the address
not the variable foo itself

i think the right way to make a pointer to foo is
Code:
*foo_ptr=foo;

and now if we execute *foo_ptr=42

we get that &foo=42
the address of foo is now 42

?
 
Last edited:
Technology news on Phys.org
*foo_ptr = 42 sets the value of thing that foo_ptr points to = 42, not the value of &foo

edit - sorry what are you actually asking?
 
i am asking what is the meaning of this line
Code:
int *foo_ptr = &foo;
 
int *x = y;

is the same thing as

int *x;
x = y;


Some people prefer to write
int* x = y;

to more clearly indicate that it's {type} {variable} = {initializer} however, that isn't advisable because of the quirks with declaring multiple variables on the same line
int * x, y;
is the same as
int * x;
int y;


You're probably familiar with array initializers; they work the same way:
int x[3] = {1, 2, 3};

And just for the fun of a complicated example...

double *w = a, x = 0.35, (*y)(double) = &sin, z[3] = {3.0, 2.0, 1.0};
is the same thing as
double *w;
w = a;
double x;
x = 0.35;
double (*y)(double); // y is a pointer to a function
y = &sin;
double z[3];
z[0] = 3.0;
z[1] = 2.0;
z[2] = 1.0;
 
Last edited:
int *foo_ptr = &foo;
Means make a pointer foo_ptr that points to the address of the memory used by the variable foo.
So when we change the value of *foo_ptr we changing the contents of the memory referenced by foo.

Note - you can't change the address of foo after it has been declared on the stack.
 
so in that way
*foo_ptr linked to foo

when do i know if its declared on a stack?
 
When you define "int foo" you ask the compiler to make room for a integer (eg. 32 or 64bits) on the stack, the stack only exists for the scope of the function.

When you do "int *foo = new integer" you reserve space on the heap for an integer, and foo will then contain that memory address. The storage is permanent until you delete it or the program ends. You can also change the address and move the memory to somewhere else (within certain limits impossedby the operating system).
 
when yo say
double *w = a;

i don't know what's "a"

"a" need to be an address to a variable
 
'a' needs to have already been declared and needs to be the same type as the type of the pointer.

double a;
double *w=a;

a is a real variable stored on the stack, *w is just a shortcut to it.
 
  • #10
it needs to be
double *w=&a;

because *w gets the address &a in order to get to the address of the variable "a"

*w=a is just changing pointing w to some address with the value "a"(not the address of the variable "a")

?
 
  • #11
Yes sorry, should be "double *w = &a;" but the point is the same.
 
  • #12
(*y)(double) = &sin

whats that ??
you are casting the word double into a pointer type
and it doesn't go anywhere

?
 
  • #13
Code:
double (*y)(double);
is a pointer to a function, the pointer identifier (the name of the pointer variable) is y. It is a pointer to a function that take one parameter, of type double, with a return type of double.

Since sin from <math.h> has that signutare
Code:
double sin( double arg );
, we can assign its address to the pointer.
Code:
y = &sin;
The & is not necessary in the case of taking the address of a function, which mean one can equally have wrote
Code:
y = sin;
 
  • #14
transgalactic said:
(*y)(double) = &sin
whats that ??

As KTC explains it's a pointer to a function.
A pointer to function is an odd feature of 'C' that allows you to pass a function to another function. It's main use is something like sort where the sort() routine doens't know how to compare two values so you have to give it a function to do that.
Because of the way 'C' uses memory when calling functions it has to know how many and what type of arguements the function has.
 
Back
Top