What is the Correct Way to Use Pointers in C?

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Value
Click For Summary

Discussion Overview

The discussion revolves around the correct usage of pointers in the C programming language, focusing on their declaration, initialization, and the implications of manipulating pointer values. Participants explore various aspects of pointers, including their relationship with memory addresses, stack versus heap allocation, and function pointers.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about the initialization of a pointer with the line "int *foo_ptr = &foo;", questioning its meaning.
  • Another participant clarifies that "*foo_ptr = 42" sets the value at the address pointed to by foo_ptr to 42, not the address of foo itself.
  • There is a discussion about the difference between stack and heap memory, with one participant explaining that declaring "int foo" allocates memory on the stack, while "int *foo = new integer" allocates memory on the heap.
  • Participants discuss the syntax of pointer declarations, noting that "double *w = a" is incorrect unless 'a' is an address, and that it should be "double *w = &a" to correctly point to the address of 'a'.
  • One participant explains that "(*y)(double) = &sin" refers to a pointer to a function, which can be used to pass functions as arguments to other functions.
  • There is a mention of the potential confusion regarding declaring multiple variables with pointers and the implications of such declarations.

Areas of Agreement / Disagreement

Participants generally agree on the basic principles of pointer usage, but there are differing interpretations and clarifications regarding specific syntax and memory allocation concepts. The discussion remains unresolved on certain points, particularly regarding the nuances of pointer initialization and memory management.

Contextual Notes

Participants highlight limitations in understanding related to the scope of variables, the distinction between stack and heap memory, and the syntax of pointer declarations. These aspects are not fully resolved within the discussion.

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 arguments the function has.
 

Similar threads

Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
5
Views
3K
Replies
7
Views
4K
  • · Replies 17 ·
Replies
17
Views
4K
Replies
12
Views
2K
Replies
3
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
5
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K