char s[] = "asdf" and extern char *s in separate files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Newsgroup - Ann

    char s[] = "asdf" and extern char *s in separate files

    Hi gurus,

    I have two files compiled together. One file has the following global
    definition:

    char s[] = "asdf";

    Another file has the following declaration:

    extern char *s;

    Is there anything wrong here? I know that although char* s and char s[] are
    not exactly the same, and in most case, they are interchangable. However,
    when I use them in separate files, there are problems:

    When I assign the value of s to another char * type local variable like

    char *p = s;

    Instead of the pointer s itself being assigned to the p, the value pointed
    by s is assgined to p. That's certainly not my intention. I know roughly to
    say, that's because the different view from the two source files, but how is
    the detail? Has anybody here ever experienced similar problems? btw, I am
    using Visual C++. Could it be a bug of VC++?

    News - Ann


  • John Harrison

    #2
    Re: char s[] = "asdf&quot ; and extern char *s in separate files


    "Newsgroup - Ann" <news_ann@yahoo .com> wrote in message
    news:3f066d52$1 _1@rcfnews.cs.u mass.edu...[color=blue]
    > Hi gurus,
    >
    > I have two files compiled together. One file has the following global
    > definition:
    >
    > char s[] = "asdf";
    >
    > Another file has the following declaration:
    >
    > extern char *s;
    >
    > Is there anything wrong here? I know that although char* s and char s[][/color]
    are[color=blue]
    > not exactly the same, and in most case, they are interchangable. However,
    > when I use them in separate files, there are problems:[/color]

    Not exactly the same means different. The first is an array, the second is a
    pointer. Do this

    extern char s[]; // s is an array
    [color=blue]
    >
    > When I assign the value of s to another char * type local variable like
    >
    > char *p = s;
    >
    > Instead of the pointer s itself[/color]

    s is not a pointer.
    [color=blue]
    > being assigned to the p, the value pointed
    > by s is assgined to p.[/color]

    That doesn't make sense. The value 'pointed' to by s is a char, how can a
    char be assigned to a pointer?
    [color=blue]
    > That's certainly not my intention. I know roughly to
    > say, that's because the different view from the two source files, but how[/color]
    is[color=blue]
    > the detail?[/color]

    There is no detail, your code is wrong so you get incorrect behaviour.
    [color=blue]
    > Has anybody here ever experienced similar problems? btw, I am
    > using Visual C++. Could it be a bug of VC++?[/color]

    No, your code is wrong. Fix the code like I explained and if you are still
    having problems post the corrected code.
    [color=blue]
    >
    > News - Ann
    >[/color]

    john


    Comment

    Working...