memory address of *char

Discussion in 'C Programming' started by DDD, Jan 28, 2008.

  1. DDD

    DDD Guest

    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.
     
    DDD, Jan 28, 2008
    #1
    1. Advertising

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

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Jan 28, 2008
    #2
    1. Advertising

  3. DDD wrote:
    > 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.
     
    Martin Ambuhl, Jan 28, 2008
    #3
  4. DDD

    Richard Bos Guest

    DDD <> wrote:

    > 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
     
    Richard Bos, Jan 28, 2008
    #4
  5. DDD

    ppi Guest

    On Jan 28, 2:08 am, DDD <> wrote:
    > 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
     
    ppi, Jan 28, 2008
    #5
  6. ppi wrote:
    > On Jan 28, 2:08 am, DDD <> wrote:
    >> I have some codes:
    >> w_char **sFieldsUri;
    >> sFieldsUri = &(new w_char[2]);

    >
    > new w_char[2] will return a w_char*,


    Not it C, it won't. It's a syntax error.
    so far so good.

    Bullshit.
     
    Martin Ambuhl, Jan 28, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    558
  2. wwj
    Replies:
    24
    Views:
    2,520
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    480
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    588
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,058
    Ian Collins
    May 9, 2006
Loading...

Share This Page