473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

problem with value of the pointer...

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
Nov 14 '05 #1
6 1338
In article <84*************************@posting.google.com> ,
os2 <ma*******@yahoo.com> wrote:
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 * .)
--
Look out, there are llamas!
Nov 14 '05 #2
os2 wrote:
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
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #3
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
[...]
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).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4
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:

news:fr.comp.lang.c

--
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?"

Nov 14 '05 #5
(supersedes <mn***********************@YOURBRAnoos.fr>)

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:

news:fr.comp.lang.c

--
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?"

Nov 14 '05 #6
Emmanuel Delahaye wrote:
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'


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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
37
by: Patrik Huber | last post by:
Hello! I got the following Code in Assembler (NASM), which prints out "5" in realmode: mov ax, 0xB800 mov es, ax mov byte , '5' I want to do the same in gcc now, but I'm stuck. GCC...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
8
by: ashu | last post by:
lets take a look at the following code:- #include<stdio.h> #include<conio.h> struct tag{ int age; char *name; }a;
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
6
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.