Pointers

P

Potatoman

struct b{//elements};

struct b* funcfoo(){
struct b* p={0};
//do something with p
return p;


}


int main(){
struct b*p=//call funcfoo
printf(*%d%s*,p->inte,p->str);
return 0;


}


///////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\­\\\\\\\\\\

SSSSSSEEEEEGMENTATION FAULTTTTT
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\////////////////////////////////////­//////////

I am sure you know why .
 
M

mdler

Hello

The struct p in the function funcfoo is local pointer (no memory
allocated)

you can not access a null pointer!

greetings Olaf


Potatoman schreef:
 
K

Keith Thompson

mdler said:
Hello

The struct p in the function funcfoo is local pointer (no memory
allocated)

you can not access a null pointer!

It's not a null pointer. It's a pointer to a non-existent object.

You can access a null pointer, but you can't dereference it. If a
pointer points to an object that no longer exists, you can neither
access it nor dereference it (just attempting to examine the value of
the pointer invokes undefined behavior).
 
K

Keith Thompson

Potatoman said:
struct b{//elements};

struct b* funcfoo(){
struct b* p={0};
//do something with p
return p;


}


int main(){
struct b*p=//call funcfoo
printf(*%d%s*,p->inte,p->str);
return 0;


}


///////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\­\\\\\\\\\\

SSSSSSEEEEEGMENTATION FAULTTTTT
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\////////////////////////////////////­//////////

I am sure you know why .

I presume that "SSSSSSEEEEEGMENTATION FAULTTTTT" is supposed to mean
"segmentation fault". Simple straightforward language is a lot easier
to read; things like "SSSSSSEEEEEGMENTATION FAULTTTTT" aren't nearly
as cute or amusing as you think they are, nor are long meaningless
strings of "/////" or "\\\\\".

As has already been discussed, you're returning a pointer to a local
object; when the local object goes out of scope, it no longer exists,
and the pointer is invalid.

There are several ways to avoid this problem. The comp.lang.c FAQ is
at <http://www.c-faq.com/>; see questions 7.5a and 7.5b. Also, you
can return a structure by value.

In general, it's very helpful if you post actual code rather than
paraphrases. I recommend
<http://www.catb.org/~esr/faqs/smart-questions.html>.
 
P

pankaj

Potatoman said:
by the way, in what way can i fix the error withouth making it global

either make the local cpoy of struct variable static storage type from
the default automatic type or allocate space for the local copy of
struct variable by malloc.
 
S

spibou

Potatoman said:
struct b{//elements};

struct b* funcfoo(){
struct b* p={0};
//do something with p
return p;


}


int main(){
struct b*p=//call funcfoo
printf(*%d%s*,p->inte,p->str);
return 0;


}

SSSSSSEEEEEGMENTATION FAULTTTTT

Potatoman said:
By the way, in what way can i fix the error withouth making it global?
<Capitalization and question mark added by me>

If I'm guessing your intentions correctly , declaring the struct p
points
to as static will achieve what you want. But as Richard Heathfield
pointed out you have several errors. The most puzzling is
printf(*%d%s*,p->inte,p->str);
For some reason there seems to be a * where a quote should appear.

Spiros Bousbouras
 
N

Nick Keighley

Potatoman said:
struct b{//elements};

struct b* funcfoo(){
struct b* p={0};
//do something with p
return p;


}


int main(){
struct b*p=//call funcfoo
printf(*%d%s*,p->inte,p->str);
return 0;


}


///////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\­\\\\\\\\\\

SSSSSSEEEEEGMENTATION FAULTTTTT
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\////////////////////////////////////­//////////

I am sure you know why .


Compiling...
potato.c
G:\tmp\potato.c(3) : error C2032: 'funcfoo' : function cannot be member
of struct 'b'
G:\tmp\potato.c(3) : error C2143: syntax error : missing ';' before '{'
G:\tmp\potato.c(3) : error C2059: syntax error : '{'
G:\tmp\potato.c(15) : warning C4013: 'printf' undefined; assuming
extern returning int
G:\tmp\potato.c(15) : error C2059: syntax error : '%'
Error executing cl.exe.

potato.obj - 4 error(s), 1 warning(s)



now post your *actual* code
 
J

John Bode

Potatoman said:
struct b{//elements};

Why did you comment out the struct elements?
struct b* funcfoo(){
struct b* p={0};
//do something with p

Something like what? You've initialized p to be 0 -- not a whole lot
you can do with a null pointer.

(style rant: when declaring a pointer, the asterisk should be
associated with the declarator, not the type specifier -- for one
thing, that's how the grammar works, and for another, inexperienced
(and some not-so-inexperienced) programmers tend to read

T* p1, p2;

as declaring both p1 and p2 as pointers to T)
return p;


}


int main(){
struct b*p=//call funcfoo

Why is the call to funcfoo commented out?
printf(*%d%s*,p->inte,p->str);

Why are you using asterisks instead of quotes? Why didn't you show the
definitions of the inte and str members in the struct definition?

What's the point in posting a code snippet that doesn't even compile,
and then asking us what's wrong with it?
return 0;


}


///////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\­\\\\\\\\\\

SSSSSSEEEEEGMENTATION FAULTTTTT
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\////////////////////////////////////­//////////

I am sure you know why .

Yeah, because you assigned a null pointer value to p and attempted to
dereference it. Bad juju.
 

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

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top