You are not logged in.

#26 2008-11-04 12:22:21

fumbles
Member
Registered: 2006-12-22
Posts: 246

Re: A rant about C with a question at the end. :P

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

#27 2008-11-04 13:18:15

dav7
Member
From: Australia
Registered: 2008-02-08
Posts: 674

Re: A rant about C with a question at the end. :P

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. big_smile

Things are a making a lot more sense now. big_smile

-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

#28 2008-11-04 13:24:48

string
Member
Registered: 2008-11-03
Posts: 286

Re: A rant about C with a question at the end. :P

*a + 10 == a[0] + 10

Offline

#29 2008-11-04 13:46:01

dav7
Member
From: Australia
Registered: 2008-02-08
Posts: 674

Re: A rant about C with a question at the end. :P

Right. That took a double take to get, but yeah. What I thought. Thanks. tongue

-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

#30 2008-11-04 18:08:19

lloeki
Member
From: France
Registered: 2007-02-20
Posts: 456
Website

Re: A rant about C with a question at the end. :P

dav7 wrote:

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 smile

Last edited by lloeki (2008-11-04 18:18:06)


To know recursion, you must first know recursion.

Offline

#31 2008-11-04 18:26:37

string
Member
Registered: 2008-11-03
Posts: 286

Re: A rant about C with a question at the end. :P

[changed my mind, please delete this post]

Last edited by string (2008-11-04 18:28:29)

Offline

#32 2008-11-04 19:39:41

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: A rant about C with a question at the end. :P

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.

Offline

#33 2008-11-04 21:33:27

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: A rant about C with a question at the end. :P

lloeki wrote:

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)

pauldonnelly wrote:
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

#34 2008-11-05 00:08:53

Garns
Member
Registered: 2008-05-28
Posts: 239

Re: A rant about C with a question at the end. :P

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

in every case it results in abc being declared as pointer (probably to a char wink). 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

#35 2008-11-05 02:39:32

ST.x
Member
From: Sydney, Australia
Registered: 2008-01-25
Posts: 363
Website

Re: A rant about C with a question at the end. :P

Garns wrote:
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

in every case it results in abc being declared as pointer (probably to a char wink). 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.

Offline

#36 2008-11-05 06:05:54

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: A rant about C with a question at the end. :P

phrakture wrote:

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

#37 2008-11-05 17:27:04

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: A rant about C with a question at the end. :P

Garns wrote:
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

#38 2008-11-06 18:32:46

lloeki
Member
From: France
Registered: 2007-02-20
Posts: 456
Website

Re: A rant about C with a question at the end. :P

I couldn't agree more with you all WRT the char *a notation, I just wasn't clear smile

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

Board footer

Powered by FluxBB