|
Thanks jhwurmbach,
I have tested that you are correct. My further finding is when you rename the extension from .cpp to .c, then C compiler will be used and there will not be any compile error.
What magics do you think runtime or compiler is making? I am very confused why the value of address of a variable is the same as that the variable itself.
jhwurmbach wrote: But you are right, both point to the same chunk of memory.
It seems like the runtime is doing some magic with the adress-of-operator.
regards,
George
|
|
|
|
|
jhwurmbach wrote: array and buf both denote pointers to chars (char*).
In fact, array is a const char*, as you can not alter the content of the string (well, even if you could, it would be wrong to try).
Actually both are const pointers to char (i.e. const char * ). That means you cannot change their value (i.e. pointed address) while it is perfectly legal to change their content.
hence:
array++;
while:
array[0] = 'H';
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Cool CPallini,
You are correct and I have tested it.
What about your comments about buf and &buf?
I have tested that buf and &buf are of the same value -- means same effect. Here is my program to test. Any comments?
int main()
{
char buf[] = "Hello World";
int p1 = buf;
int p2 = &buf;
return 0;
}
regards,
George
|
|
|
|
|
|
CPallini wrote: And (surprisingly to me) they indeed hold the same address.
I am glad that I am not the only one who is surprised.
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all. Douglas Adams, "Dirk Gently's Holistic Detective Agency"
|
|
|
|
|
Often a little test is worthy to do.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
I did[^], but it was you who found the bit of explanation.
Thanks!
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all. Douglas Adams, "Dirk Gently's Holistic Detective Agency"
|
|
|
|
|
|
George_George wrote: The address of operator (& does nothing when applied to array names (see discussion below), but does with pointers..
Yes. The overall point is it.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Thanks for your confirmation CPallini!
regards,
George
|
|
|
|
|
Your examples aren't all that correct. But I think I get the idea of the question.
This will be brief. For a longer version, read a C book. The Kernigan & Richie one taught me loads in a small book.
char array [] = "hello world";
ends up with a constant pointer to chunk of memory holding hello world followed by a zeroed character (NULL).
Same with buf , but it's a constant pointer to a chunk of memory 256 characters long - which could hold anything.
buf is a pointer.
buf [37] is the value in the 37th character in the chunk of memory pointed to by buf .
array[buf] would be the character pointed to by array + an offset of the amount of buf. If buf = 0x12344, this is a big offset.
array(buf) will get the compiler moaning at you, because array is not a function.
Going to array...
array is a pointer to the start of memory holding "hello world".
array[4] is the 4th character in that array. ie 'o', not 'l'.
*array is the character pointed to by array, ie 'h'.
*array and array [0] are equivalent.
array+4 is the pointer to the memory of helloe world, with 4 added to it. So it points to 'o'.
&array[4] is the pointer to the 4th char in the array. same as above.
So...
&array[x] is the same as array+x. The only difference is how similar it looks to the code around it. If the memory is allocated as an array, and that's how you're working on it, &array[0] may be more readable.
If you working on pointers to structures, and your function doesn't know how they're allocated, using array may be more readable.
I hope this helps a little!
Iain.
|
|
|
|
|
Thanks Iain,
How about array and &array, are they always the same?
(in your reply, I have not seen any discussion about &array).
regards,
George
|
|
|
|
|
Erm, no, they're never the same. That's like asking "Is 'Bob' the same as '#1 The Street, Townsville'?"
The & means pointer to.
So, &array is a pointer to a pointer to chars.
This can make the brain hurt, but is very useful when you get the hang of it.
Iain.
|
|
|
|
|
Hi Iain,
I have tested that buf and &buf are of the same value -- means same effect. Here is my program to test. Any comments?
int main()
{
char buf[] = "Hello World";
int p1 = buf;
int p2 = &buf;
return 0;
}
regards,
George
|
|
|
|
|
CPallini's answer was good, and correct.
All it means though is the value of array and &array are the same as C nicely hacks it that way.
BUT...
They are of different types, as no doubt C will complain if you try to do this...
char array [] = "hello world";
char *p;
char **pp
p = array;
ASSERT(*p == 'h';
p = &array;
pp = &array;
ASSERT(**p == 'h');
Getting there?
Iain.
|
|
|
|
|
Actually:
Iain Clarke wrote: char array [] = "hello world";
char *p;
char **pp
p = array; // happy
ASSERT(*p == 'h';
p = &array; // not happy.
is OK, while
Iain Clarke wrote:
char **pp;
pp = &array;
ASSERT(**p == 'h');
is NOT so OK!
It is a very tricky topic...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Hi Iain,
I have tested that this line,
pp = &array;
also can not compile. I am using Visual Studio 2008 and C compiler.
regards,
George
|
|
|
|
|
I think Cpallini has pretty thoroughly covered the whole field of pointers to arrays.
And while my answers may have been longer, his are that little bit more accurate with respect to &array.
If it doesn't compile, then the error message you get should be worth a read.
It's probably worth understanding pointers before you go back to socket questions!
Iain.
|
|
|
|
|
Thanks for your advice, Iain!
regards,
George
|
|
|
|
|
Iain Clarke wrote: So, &array is a pointer to a pointer to chars.
Your argument is good when applied on standard pointers. On the other hand, the address-of operator has a strange behaviour whenever is applied to array identifiers.
See, for instance.
http://www.fredosaurus.com/notes-cpp/arrayptr/arraysaspointers2.html[^]
You can also make a little test.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
&array is a pointer to an array pointer:
int values[] = { 10, 20, 30, 40, 55, 60, 70, 80, 90, 100 };
int (*pval)[10] = &values;
wcout << *pval[0] << endl;
wcout << **pval << endl;
"We make a living by what we get, we make a life by what we give." --Winston Churchill
|
|
|
|
|
Thanks George,
I do not understand why **pval is the same as *pval[0]? I think *pval should be the same as *pval[0]. But during testing, you are correct. Why?
regards,
George
|
|
|
|
|
pval[0] is a pointer to the first value and *pval[0] is the first value. pval is a pointer-to-a-array pointer. *pval dereferences to an array pointer and **pval dereferences to the first value. So, to get the second value using both:
wcout << *(*pval + 1) << endl;
wcout << *(pval[0] + 1) << endl;
"We make a living by what we get, we make a life by what we give." --Winston Churchill
|
|
|
|
|
Cool, thanks George!
regards,
George
|
|
|
|
|
George_George wrote: Sometimes, I noticed that we either use,
1. array (buf)
or use,
2. &array (&buf)
or use
3. &array[0] (&buf[0])
Where have you seen this convention? I certainly don't profess to being a C expert, but I've never used nor have I ever seen this syntax before.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|