6
Contributors
13
Replies
17
Views
5 Years
Discussion Span
Last Post by jnawrocki
0

if n is a pointer i.e. int n*; and m is a simple integer i.e int m; then you use n=&m to store the memory address of m into n. And you use *n=m to store the value of m into n.
* is called the Value at Operator
& is called the Address of Operator

0

Maybe this will clear it up:

void main(void)
{ 
	int *p, i;         // integer i & uninitialized pointer to an integer *p
	i = 10;            // put a value in i
	p = &i;            // initialize the pointer *p to the address of i
	printf("%d\n",*p); // printf the value stored in i by using the address of i
                            //    stored in p  
}

Edited by Narue: Added code tags

0

the point is the code that I posted gives an error
why?

and somtimes the other exp gives an error...
is it compiler dependent? I used GNU gcc compiler and the code is in c(not c++)

0

the point is the code that I posted gives an error
why?

Because your code is wrong.

and somtimes the other exp gives an error...

Because your code is wrong.

is it compiler dependent?

No, your code is just wrong.

Read this and this.

0

This is how it works for me:

#include <stdio.h>
#include <stdlib.h>

int main(){
    int *n, m = 2;
    *n = 5;
    printf("Numbers *n: %i m: %i", *n, m);
    n = &m;
    printf("\nNumbers *n: %i m: %i", *n, m);
    return EXIT_SUCCESS;
}

Edited by pyTony: n/a

0
int *n, m = 2;
*n = 5;

n is an uninitialized pointer, yet you dereference it. The code is wrong, and "works for me" is not synonymous with "correct".

0

n is an uninitialized pointer, yet you dereference it. The code is wrong, and "works for me" is not synonymous with "correct".

Then I would initalize dummy variable:

#include <stdio.h>
#include <stdlib.h>

int main(){
    int dummy = 5, m = 2;
    int * n = &dummy;
    printf("Numbers *n: %i m: %i", *n, m);
    n = &m;
    printf("\nNumbers *n: %i m: %i", *n, m);
    m = 3;
    printf("\nNumbers *n: %i m: %i", *n, m);
    return EXIT_SUCCESS;
}

Edited by pyTony: n/a

0

So then how do I store the value of a variable in another variable that is pointed to by a pointer? in other words how do i change the value stored at *n to the value of m?
(and I dont want n to point to the same address as that of m... i.e. change in one should not effect the other)

I did go through the link eternallyconfuzzled ( an excellent tutorial on trees I must say that I had been following even before it (the link) was posted here) but could not find the answer and the "correct" way to do so.

0

// Not Code
// From my previous post:

int *p, i; // integer i & uninitialized pointer to an integer *p
i = 10; // put a value in i
p = &i; // initialize the pointer *p to the address of i
//
// You can now use *p to get the value stored in i i.e.
//
printf("%d\n",*p); // print the value in i using the address of i in *p

0

"p" will not change unless you change it. It will always contain the address of "i". Changing the contents of "i" has no effect on "p". You might use this pointer when looping through a table loading "i" with members of an array and the doing something with each member. "p" is always pointing to "i" but "i" changes as you load each member of the array into "i".

Edited by jnawrocki: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.