You are not logged in.
Real men know Visual Basic!
jk =p
int b;
int a = 2;
int *ptr; <== that declares a pointer
ptr = &a; <== address of a store in pointer ptr
b = *ptr; <== dereference (get the value of the thing its pointing too) pointer ptr and store into varible b
printf("%d %d %d %p", a, b, *ptr, ptr); // displays 2 2 2 [some mem address]
Also:
a[10] is the same as *(a+10)
[EDIT] Whoops!
Last edited by fumbles (2008-11-05 01:57:34)
Offline
Ah, I see.
Don't you mean *(a + 10)?
And is it because of syntax of fundamentals that you can't do *a + 10? I know you can't (for obvious reasons), but I just want to understand why.
Also, thanks for this help.
Things are a making a lot more sense now.
-dav7
Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.
Offline
*a + 10 == a[0] + 10
Offline
Right. That took a double take to get, but yeah. What I thought. Thanks.
-dav7
Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.
Offline
So *hello = "hello world" is NOT 11 single pointers to single chars, it's one pointer to 11 chars?
no, it's ONE pointer, that is ONE address to ONE location in memory (which is redundant to say). it just "happens" that there are eleven chars there, but you have no means to know it unless you increment the pointer up to the \0 (which is what strlen does).
since [] is just a notation to pointer arithmetics, you could write hello[13] or hello[-4] and it will give you a value (which happens to be what is in RAM there).
by the way when you declare a pointer it's 'char* abc', not 'char *abc'. again, 'whatever*' is just a notation for 'long' or whatever a pointer is on the machine you're compiling. and some help to the parser so that it gives you some type warnings (but they really don't matter).
oh and chars are just bytes too.
if x == 'a' and you do x=x+5, that makes x == 'f'.
all that 'type' stuff is just interpretation of a bunch of numbers that are stored in memory, where you cain point to, then read and write. once you get that, you get C. and can have fun at pointing to pointers that point to pointers. really
Last edited by lloeki (2008-11-04 18:18:06)
To know recursion, you must first know recursion.
Offline
[changed my mind, please delete this post]
Last edited by string (2008-11-04 18:28:29)
Offline
pauldonnelly: in as much as "int *foo" is a pointer to one int, so is "char *bar" a pointer to one char. As I said before, and I will bold it this time:
I don't understand what you're telling me.
Offline
since [] is just a notation to pointer arithmetics, you could write hello[13] or hello[-4] and it will give you a value (which happens to be what is in RAM there).
And in theory 4[hello] actually works, considering x[y] is just shorthand for *(x+y)
phrakture wrote:pauldonnelly: in as much as "int *foo" is a pointer to one int, so is "char *bar" a pointer to one char. As I said before, and I will bold it this time:
I don't understand what you're telling me.
I'm saying that "char *" is still just one pointer. It may point to a chunk of memory that is 800 characters long, but the pointer is still one pointer.
printf("char* => %d\n", sizeof(char*));
printf(" int* => %d\n", sizeof(int*));
They should be the same. A pointer is a pointer. Now the DIFFERENCE is only what they point to. In this case:
char *foo = "hello whirled";
printf("sizeof(char*)char* => %d\n", sizeof(char*));
printf(" sizeof(foo) => %d\n", sizeof(foo));
printf(" strlen(foo) => %d\n", strlen(foo));
It might work to think of "foo" in the above example as an iterator, and not a string. It is an iterator that points to the beginning of a sequence, with the end of the sequence being NULL. Even better: think of it like a linked list you may be used to in Java or C++. All you have is the head node. To get the last node, or the length, or something, you have to look until you find the end. So, we can do the following:
for(char *foo = "hello whirled"; *foo; foo++) {
printf("%c\n", foo);
while *foo, which gives us the CURRENT character that foo is pointing to, is not NULL, loop and increment
The above is actually fairly common for string routines.
Offline
by the way when you declare a pointer it's 'char* abc', not 'char *abc'. again, 'whatever*' is just a notation for 'long' or whatever a pointer is on the machine you're compiling. and some help to the parser so that it gives you some type warnings (but they really don't matter).
Correct me if I am wrong, but they way I read the standard * is not part of the type, but is part of the declarator. As spaces are to some extent optional you can write
char *abc
or
char* abc
or even
char*abc
in every case it results in abc being declared as pointer (probably to a char ). However writing char *abc is seen as helpful by some people, because it avoids misunderstanding:
char* a,b;
in this case a is a pointer, while b is a char, which is not immediately obvious. This becomes a lot clearer when written:
char *a,b;
Offline
lloeki wrote:by the way when you declare a pointer it's 'char* abc', not 'char *abc'. again, 'whatever*' is just a notation for 'long' or whatever a pointer is on the machine you're compiling. and some help to the parser so that it gives you some type warnings (but they really don't matter).
Correct me if I am wrong, but they way I read the standard * is not part of the type, but is part of the declarator. As spaces are to some extent optional you can write
char *abc
or
char* abc
or even
char*abcin every case it results in abc being declared as pointer (probably to a char ). However writing char *abc is seen as helpful by some people, because it avoids misunderstanding:
char* a,b;
in this case a is a pointer, while b is a char, which is not immediately obvious. This becomes a lot clearer when written:
char *a,b;
+1, yeah char *abc is the most common way for clarity and style.
ARCH64 | XMonad | Configs | myAURpkgs | ArchWiki Contribs | Screenies
Offline
I'm saying that "char *" is still just one pointer. It may point to a chunk of memory that is 800 characters long, but the pointer is still one pointer.
Ah, okay. Like I said.
Offline
lloeki wrote:by the way when you declare a pointer it's 'char* abc', not 'char *abc'. again, 'whatever*' is just a notation for 'long' or whatever a pointer is on the machine you're compiling. and some help to the parser so that it gives you some type warnings (but they really don't matter).
Correct me if I am wrong, but they way I read the standard * is not part of the type, but is part of the declarator. As spaces are to some extent optional you can write
char *abc
or
char* abc
or even
char*abc
All work just fine because of the way the source is tokenized. * can't be part of a name or keyword. That is, however, just a side-effect. As for actual USAGE, there are two camps.
The C++ people generally prefer "char* foo" because "char*" is the type of "foo". This causes the typical issues with "char* a,b;" you mentioned, but in general, pointers aren't used too heavily in good C++.
The C people generally "char *foo" because "*foo" is a pointer. "To what?" you ask? "char".
It's more about how it reads. Both are completely fine, and it's all preference. It's like a tabs vs spaces argument. Personally, I've come to love the C way, even when doing C++
Offline
I couldn't agree more with you all WRT the char *a notation, I just wasn't clear
the only thing I wanted to highlight was the meaning of it behind the scenes, that is "a pointer is an int and not some magic structure that knows what it points to".
Last edited by lloeki (2008-11-06 18:33:30)
To know recursion, you must first know recursion.
Offline