Thread: pointer help

1. pointer help

I'm trying to learn pointers and their values, i know the following (please correct if incorrect)

ptr = the value of the variable it points to
*ptr = the address of the variable it points to
&ptr = the address of the pointer it self

but what does *&ptr mean? i see this often and have no clue what it means. can you help me?

2. > ptr = the value of the variable it points to
> *ptr = the address of the variable it points to
Actually if ptr is a pointer then
ptr = address of the variable it points to
*ptr = the value of the variable it points to

- Sean

3. so then what would *&ptr be? the value at the address of ptr?

4. * and & are the inverse of one another, so they cancel out, and *&ptr = ptr.

5. so what if ptr is a string not a pointer? would i be accessing ptr as a pointer if i did *(long *)&ptr = something;?

6. Not sure if this is what you mean, but you access a string by a pointer to the beginning of the string.

7. no i mean if i have the following code
Code:
```#include <stdio.h>

int main()
{
char a_string[1030];
int i;
for(i=0;i<=1030;i++)
{
printf("The *& value of a_string[%i]:  %x", i , a_string);
}

};```
what value am i getting there and whats the diffrence between that and
just normal buffer[i] ?

8. char a_string[1030];
int i;
for(i=0;i<=1030;i++)
This is wrong. <= 1030 will end up pointing PAST the end of your array. Arrays go from zero, to SIZE-1, not from zero to SIZE. This should be "<1030".

what value am i getting there and whats the diffrence between that and
just normal buffer[i] ?
So why don't you add a few more lines to your code and find out for yourself?

Furthermore, your printf is wrong. It will only ever print the initial string pointer. Your loop doesn't ever change what value it prints.

Quzah.

9. well thank you for critisizing my code when it wasnt even in question. thats not code that i've actually tested, and i wrote it on the fly and do expect it to have errors. i did write code to find the diffrence. there is a really wierd diffrence here's the ACTUALL code.

Code:
```#include <stdio.h>

long get_sp()
{
__asm__("movl %esp, %eax");
}
int main()
{
char string[1030];
int i;
long retaddy;

fprintf(stderr, "retaddy: %x\n", retaddy);

retaddy=get_sp();

for(i=0;i<20;i++)
{
*(long *)&string[i]=retaddy;
fprintf(stderr, "*&string[%i]: %x\n",i ,*(long *)&string[i]);
}

for(i=0;i<20;i++)
{
(long *)string[i]=retaddy;
fprintf(stderr, "(long *)string[%i]: %x\n",i , (long *)string[i]);
}

};```

this gives me the following:
Code:
```*&string[0]: bffff4ac
*&string[1]: bffff4ac
*&string[2]: bffff4ac
*&string[3]: bffff4ac
*&string[4]: bffff4ac
*&string[5]: bffff4ac
*&string[6]: bffff4ac
*&string[7]: bffff4ac
*&string[8]: bffff4ac
*&string[9]: bffff4ac
*&string[10]: bffff4ac
*&string[11]: bffff4ac
*&string[12]: bffff4ac
*&string[13]: bffff4ac
*&string[14]: bffff4ac
*&string[15]: bffff4ac
*&string[16]: bffff4ac
*&string[17]: bffff4ac
*&string[18]: bffff4ac
*&string[19]: bffff4ac
(long *)string[0]: ffffffac
(long *)string[1]: ffffffac
(long *)string[2]: ffffffac
(long *)string[3]: ffffffac
(long *)string[4]: ffffffac
(long *)string[5]: ffffffac
(long *)string[6]: ffffffac
(long *)string[7]: ffffffac
(long *)string[8]: ffffffac
(long *)string[9]: ffffffac
(long *)string[10]: ffffffac
(long *)string[11]: ffffffac
(long *)string[12]: ffffffac
(long *)string[13]: ffffffac
(long *)string[14]: ffffffac
(long *)string[15]: ffffffac
(long *)string[16]: ffffffac
(long *)string[17]: ffffffac
(long *)string[18]: ffffffac
(long *)string[19]: ffffffac```
i want to know why they are (almost) completly diffrent.

10. Your casting of everything to longs is confusing the issue, I believe. By doing this with the fprintf()'s, the program is printing erroneous data.

To simplify, I'd suggest you make this:
>fprintf(stdout, "*&string[%i]: %x\n", i, *(long *)&string[i]);
into this:
>fprintf(stdout, "*&string[%i]: %x\n", i, *(&string[i]));

Here's a sample program to help clarify (hopefully):
Code:
```#include <stdio.h>

int main(void)
{
char astring[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

printf ("astring >%s<\n", astring);
printf ("Element 11 located @ %p, contains: 0x%02x %c\n", &astring[10], astring[10], astring[10]);
printf ("Accessed differently, contains is 0x%02x %c\n", *(&astring[10]),*(&astring[10]));
printf ("Accessed incorrectly, contains is 0x%02x %c\n", *(long *)&astring[10],*(long *)&astring[10]);

return 0;
}

/* Output from this program:
astring >ABCDEFGHIJKLMNOPQRSTUVWXYZ<
Element 11 located @ 0064FDEE, contains: 0x4b K
Accessed differently, contains is 0x4b K
Accessed incorrectly, contains is 0x4e4d4c4b K
*/```

Popular pages Recent additions