memory address of *char

D

DDD

I have some codes:
w_char **sFieldsUri;
sFieldsUri = &(new w_char[2]);

w_char **sValFind = nsnull;
sValFind = &(new w_char[2]);

When I debug my program, I found the above code had some strange
things. sFieldsUri+1 is equal to sValFind+0.

But if I change the codes to the following:
w_char **sFieldsUri;
sFieldsUri = &(new w_char[2]);

for(int i=0; i<2; i++)
{
sFieldsUri = new w_char[100];
}

w_char **sValFind = nsnull;
sValFind = &(new w_char[2]);

for(int i=0; i<2; i++)
{
sVal Find = new w_char[100];
}
sFieldsUri+0, sFieldsUri+1, and sValFind+0, sValFind+1 are all
difference.
Thanks for all.
 
R

Richard Heathfield

DDD said:
I have some codes:
w_char **sFieldsUri;
sFieldsUri = &(new w_char[2]);

The language rules for C and C++ differ. I suggest you ask this C++
question in comp.lang.c++ for best results.
 
M

Martin Ambuhl

DDD said:
I have some codes:
w_char **sFieldsUri;
sFieldsUri = &(new w_char[2]);
^^^^^^^^^
This tells us that you are in the wrong place, since that is a syntax
error in C. Try posting to a newsgroup for whatever language you are using.
 
R

Richard Bos

DDD said:
I have some codes:
w_char **sFieldsUri;
sFieldsUri = &(new w_char[2]);

This is not C, but (probably!) C++. Pointer handling in C and C++ are
sufficiently different that you should ask this in comp.lang.c++,
because any answer you get here will be given from a C POV, and might
therefore not apply to C++.

Richard
 
P

ppi

I have some codes:
w_char **sFieldsUri;
sFieldsUri = &(new w_char[2]);

new w_char[2] will return a w_char*, so far so good. Except you are
trying to take the address of non lvalue ...
Enable ALL warning/error messages from your compiler, you should get
an error.
&(new w_char[2]) is really weird, really (besides from taking the
adress of non lvalue) it is like wrting int* p = &3;
w_char **sValFind = nsnull;
sValFind = &(new w_char[2]);

When I debug my program, I found the above code had some strange
things. sFieldsUri+1 is equal to sValFind+0.

I am still surprised that your compiler can compile this.
But if I change the codes to the following:
w_char **sFieldsUri;
sFieldsUri = &(new w_char[2]);

forget it.
for(int i=0; i<2; i++)
{
sFieldsUri = new w_char[100];
}

w_char **sValFind = nsnull;
sValFind = &(new w_char[2]);

for(int i=0; i<2; i++)
{
sVal Find = new w_char[100];
}
sFieldsUri+0, sFieldsUri+1, and sValFind+0, sValFind+1 are all
difference.
Thanks for all.


If I were a gambler I would say you want that:

w_char **sFieldsUri;
sFieldsUri = new w_char*[2]; // notice the '*' thing

and try to re-run/compile your program.

you should get a decent compiler dude.

Cheers,
Paulo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top