Unix Programming - char*

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > April 2005 > char*





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author char*
Russell Shaw

2005-04-17, 7:54 am

Hi,

If i have: char *str = "test" in one .c file, is it guaranteed
that str == "test" in any other .c file after linking?
(ie: there isn't multiple "test" strings stored in the final object)
Måns Rullgård

2005-04-17, 7:54 am

Russell Shaw <rjshawN_o@s_pam.netspace.net.au> writes:

> Hi,
>
> If i have: char *str = "test" in one .c file, is it guaranteed
> that str == "test" in any other .c file after linking?
> (ie: there isn't multiple "test" strings stored in the final object)


There's generally no guarantee, but with the right linker flags, you
might be able to get that result. Don't depend on it.

--
Måns Rullgård
mru@inprovide.com
Ravi Uday

2005-04-19, 2:49 am



Russell Shaw wrote:
> Hi,
>
> If i have: char *str = "test" in one .c file, is it guaranteed
> that str == "test" in any other .c file after linking?
> (ie: there isn't multiple "test" strings stored in the final object)


No.

In C -
If you need to do that, then you need to declare 'str' as an 'external'
keyword in your second file or
have the declaration placed in a header file and include the header
file in all the places where you like to use str or
declare it as global and access in other files
You might consider reading C-FAQ
Q. No. 1.7: What's the best way to declare and define global variables
and functions?

@ http://www.eskimo.com/~scs/C-faq/top.html


HTH,
-Ravi

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com