Scoping question in C

Jian (James) Zhou jameszhou2000-PkbjNfxxIARBDgjK7y7TUQ at public.gmane.org
Fri Jun 10 20:46:13 UTC 2005


Just to add a few more comments. Constants is a special case. A constant
means it is read-only, and it usually resides in text segment. Thus, if you
assign a constant string to the pointer "x" inside the function, I don't
think there will be any problem to access it even after the function is
returned. However, I do not think this is a good practice since originally
there is no information saying the pointer "x" is pointing to a constant
string. Therefore, a later programmer might have troubles to maintain or
read this code because he may not know that the string can not be modified.

James Zhou



----- Original Message ----- 
From: "William Park" <opengeometry-FFYn/CNdgSA at public.gmane.org>
To: <TLUG-lxSQFCZeNF4 at public.gmane.org>
Sent: Thursday, June 09, 2005 6:48 PM
Subject: [TLUG]: Scoping question in C


> I have question regarding scope of static string inside C function.
> Suppose I have
>
>     char *x;
>
>     int func ()
>     {
> x = "something";
> return (1);
>     }
>
> where 'x' is global variable being used elsewhere.  Can I use 'x' after
> func() exits?  That is, is 'x' still pointing to string "something"?
>
> -- 
> William Park <opengeometry-FFYn/CNdgSA at public.gmane.org>, Toronto, Canada
> ThinFlash: Linux thin-client on USB key (flash) drive
>    http://home.eol.ca/~parkw/thinflash.html
> --
> The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
> TLUG requests: Linux topics, No HTML, wrap text below 80 columns
> How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml
>
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list