473,397 Members | 1,949 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,397 software developers and data experts.

simple problem


the c code as follows:

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

int main()
{
printf("%s\n", getenv("NAME"));
return 0;
}

Segmentation Fault

why?

cheers,

Nov 14 '05 #1
4 1329
On Fri, 9 Apr 2004 23:51:26 GMT, J Wang <cs****@bath.ac.uk> wrote:

the c code as follows:

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

int main()
{
printf("%s\n", getenv("NAME"));
return 0;
}

Segmentation Fault

why?
NULL.
-leor

cheers,

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #2
"J Wang" <cs****@bath.ac.uk> wrote in message
news:Pi**************************************@amos .bath.ac.uk...

the c code as follows:

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

int main()
{
char *name = getenv("NAME");
printf("%s\n", getenv("NAME"));
if(name)
printf("%s\n", name);
else
printf("%s\n", "getenv() returned NULL");
return 0;
}

Segmentation Fault

why?


Most likely you tried to dereference a NULL pointer.

*Always* check the return value of a function
which can fail.

-Mike
Nov 14 '05 #3
J Wang wrote:
the c code as follows:

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

int main()
{
printf("%s\n", getenv("NAME"));
return 0;
}

Segmentation Fault

why?


Perhaps "NAME" is not defined in the environment, so getenv() returns
NULL and your attempt to dereference it is illegal. We just had this a
couple of days ago. That you did not see it shows that you violated
usenet etiqutte by posting without following the newsgroup first. You
probably didn't check the FAQ either.
Nov 14 '05 #4
On Fri, 9 Apr 2004 23:51:26 GMT, J Wang <cs****@bath.ac.uk> wrote:

the c code as follows:

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

int main()
{
printf("%s\n", getenv("NAME"));
return 0;
}

Segmentation Fault

why?


getenv is allowed to return NULL. You need to check for that
situation before dereferencing the return value.
<<Remove the del for email>>
Nov 14 '05 #5

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

Similar topics

3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
6
by: francisco lopez | last post by:
ok , first of all sorry if my english is not so good, I do my best. here is my problem: I don´t know much javascript so I wrote a very simple one to validate a form I have on my webpage. ...
0
by: 42 | last post by:
I implemented a simple class inherited from Page to create a page template. It simply wraps some trivial html around the inherited page, and puts the inherited page into a form. The problem I...
18
by: Sender | last post by:
Yesterday there was a very long thread on this query. (You can search on this by post by 'sender' with subject 'Simple Problem' post date Oct 7 time 1:43p) And in the end the following code was...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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,...

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.