What is the Correct Way to Use Pointers in C?

  • Thread starter transgalactic
  • Start date
  • Tags
    Value
In summary: So when you assign a pointer to a function it takes the address of the function, casts it to the correct type (double in this case), and assigns it to the pointer.
  • #1
transgalactic
1,395
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
  • #2
*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?
 
  • #3
i am asking what is the meaning of this line
Code:
int *foo_ptr = &foo;
 
  • #4
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:
  • #5
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.
 
  • #6
so in that way
*foo_ptr linked to foo

when do i know if its declared on a stack?
 
  • #7
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).
 
  • #8
when yo say
double *w = a;

i don't know what's "a"

"a" need to be an address to a variable
 
  • #9
'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.
 

FAQ: What is the Correct Way to Use Pointers in C?

What is a pointer value?

A pointer value is a variable that contains the address of another variable or object in computer memory. It allows a program to indirectly access and modify the value of the variable or object it points to.

How is a pointer value different from a regular variable?

A pointer value is different from a regular variable because it stores a memory address instead of a specific value. This allows for more flexibility in accessing and modifying variables and objects in a program.

Why are pointer values important in programming?

Pointer values are important in programming because they allow for dynamic memory allocation and efficient data structures. They also enable functions to modify variables and objects outside of their own scope.

How do I declare and initialize a pointer value in my program?

To declare and initialize a pointer value, you need to use the asterisk (*) symbol before the variable name, followed by the assignment operator (=) and the address of the variable or object you want to point to. For example: int *ptr = # where "ptr" is the pointer variable and "num" is the variable it points to.

What are some common mistakes when using pointer values?

Some common mistakes when using pointer values include dereferencing a null pointer, causing a segmentation fault, and not properly deallocating dynamically allocated memory, leading to memory leaks. It is important to carefully manage and check pointer values to avoid these errors.

Back
Top