problem with value of the pointer...

O

os2

hi

i would like to try to reduce global variable for a program...
i get a value from getenv... after this value will never change... i
tried this code:

void readRtuConfig(char *local_dir_led)
{
if(!(local_dir_led = getenv("LOCAL_DIR_LED")))
{
printf("Incapable de lire la variable: LOCAL_DIR_LED\n");
return 1;
}
printf("%s\n", local_dir_led);
}

void main()
{
char *local_dir_led=NULL;
readRtuConfig(local_dir_led);
printf("%s\n", local_dir_led);
}

the program crash...

maybe that happen because local_dir_led value don't exit outside
readRtuConfig... and try to prinf a value who have nothing?

any idea to resolve that?

thanks
 
W

Walter Roberson

os2 said:
void readRtuConfig(char *local_dir_led)
{
if(!(local_dir_led = getenv("LOCAL_DIR_LED")))
{
printf("Incapable de lire la variable: LOCAL_DIR_LED\n");
return 1;
}
printf("%s\n", local_dir_led);
}

Your change to local_dir_led is not going to be propagated upwards
to the calling function. If you want to get the string pointer upwards
then you either need to return it from the function, or you need to
have the calling routine pass in an address that you can write the
value into.

void main()

main() is never of void return type. main() is always of int return
type, with C89 allowing you to not actually return anything [but
not defining what the exit status is in such a case.] If I understand
properly, C99 requires that you return something out of main().
{
char *local_dir_led=NULL;
readRtuConfig(local_dir_led);
printf("%s\n", local_dir_led);
}

You are passing a copy of the -value- NULL into the routine
as a parameter. That routine overwrites the copy to its content, but
doesn't push the new value back to the calling routine. So after the
routine has been called, local_dir_led still has the value NULL,
and your program crashes trying to print the string at that [usually]
non-existant location.
any idea to resolve that?

Learn how to pass in addresses of variables and how to then write
new values in the variables in the called routine. (This idea
is brought to you today by the characters & and * .)
 
A

Artie Gold

os2 said:
hi

i would like to try to reduce global variable for a program...
i get a value from getenv... after this value will never change... i
tried this code:

void readRtuConfig(char *local_dir_led)
{
if(!(local_dir_led = getenv("LOCAL_DIR_LED")))
As C passes *by value* `local_dir_led', which is local to this function,
now contains the value returned by getenv().
{
printf("Incapable de lire la variable: LOCAL_DIR_LED\n");
return 1;
}
printf("%s\n", local_dir_led);
}

void main() int main(void)
{
char *local_dir_led=NULL;
local_dir_led contains the value NULL.
readRtuConfig(local_dir_led);
local_dir_led *still* contains the value NULL...
printf("%s\n", local_dir_led); ....so you crash here.
}

the program crash...

maybe that happen because local_dir_led value don't exit outside
readRtuConfig... and try to prinf a value who have nothing?

any idea to resolve that?

thanks

You have two options:

1) Change the signature of readRtuConfig() to accept a pointer to a
pointer to char (char **) and modify its dereferenced value.

2) Have readRtuConfig return the value of the getenv() call.

HTH,
--ag
 
K

Keith Thompson

main() is never of void return type. main() is always of int return
type, with C89 allowing you to not actually return anything [but
not defining what the exit status is in such a case.] If I understand
properly, C99 requires that you return something out of main().

Alas, no. In C99, falling off the end of main() without returning a
value is equivalent to doing a "return 0;" (making things a little
easier for sloppy programmers by creating a special-case rule that
doesn't apply to any other function).
 
E

Emmanuel Delahaye

os2 wrote on 06/05/05 :
void readRtuConfig(char *local_dir_led)
{
if(!(local_dir_led = getenv("LOCAL_DIR_LED")))

Modifying a local variable often means 'bad design'
{
printf("Incapable de lire la variable: LOCAL_DIR_LED\n");
return 1;
}
printf("%s\n", local_dir_led);
}

void main()

main() return int. Always.
{
char *local_dir_led=NULL;
readRtuConfig(local_dir_led);
printf("%s\n", local_dir_led);

The function modifies nothing. You are passing NULL to printf() with
"%s". The behaviour is undefined.

#include <stdlib.h>
#include <stdio.h>

char *readRtuConfig (void)
{
char *local_dir_led = getenv ("LOCAL_DIR_LED");
if (local_dir_led != NULL)
{
printf ("%s\n", local_dir_led);
}
else
{
printf ("Incapable de lire la variable: LOCAL_DIR_LED\n");
}
return local_dir_led;
}

int main (void)
{
char *local_dir_led = readRtuConfig ();

if (local_dir_led != NULL)
{
printf ("%s\n", local_dir_led);
}
return 0;
}

Feel free to ask for details.

Note that there is also a French-speaking newsgroup for the C-language:


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
 
E

Emmanuel Delahaye

(supersedes <[email protected]>)

os2 wrote on 06/05/05 :
void readRtuConfig(char *local_dir_led)
{
if(!(local_dir_led = getenv("LOCAL_DIR_LED")))

Modifying a parameter often means 'bad design'
{
printf("Incapable de lire la variable: LOCAL_DIR_LED\n");
return 1;
}
printf("%s\n", local_dir_led);
}

void main()

main() return int. Always.
{
char *local_dir_led=NULL;
readRtuConfig(local_dir_led);
printf("%s\n", local_dir_led);

The function modifies nothing. You are passing NULL to printf() with
"%s". The behaviour is undefined.

#include <stdlib.h>
#include <stdio.h>

char *readRtuConfig (void)
{
char *local_dir_led = getenv ("LOCAL_DIR_LED");
if (local_dir_led != NULL)
{
printf ("%s\n", local_dir_led);
}
else
{
printf ("Incapable de lire la variable: LOCAL_DIR_LED\n");
}
return local_dir_led;
}

int main (void)
{
char *local_dir_led = readRtuConfig ();

if (local_dir_led != NULL)
{
printf ("%s\n", local_dir_led);
}
return 0;
}

Feel free to ask for details.

Note that there is also a French-speaking newsgroup for the C-language:


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
 
C

CBFalconer

Emmanuel said:
os2 wrote on 06/05/05 :


Modifying a parameter often means 'bad design'

Not a good rule of thumb. A parameter is actually an externally
initialized local variable, and should be treated as such. If you
can't tell whether you need the original value later, then you have
a bad design, and probably an overly long and overly complex
function.
 

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

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top