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

  • Thread starter Newsgroup - Ann
  • Start date
N

Newsgroup - Ann

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
 
J

John Harrison

Newsgroup - Ann said:
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:

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
When I assign the value of s to another char * type local variable like

char *p = s;

Instead of the pointer s itself

s is not a pointer.
being assigned to the p, the value pointed
by s is assgined to p.

That doesn't make sense. The value 'pointed' to by s is a char, how can a
char be assigned to a pointer?
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?

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

No, your code is wrong. Fix the code like I explained and if you are still
having problems post the corrected code.
News - Ann

john
 

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

Staff online

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top