segementation fault

P

priyanka

Hi there,

I am trying to compare the two strings but I get segmentation fault.

const char *function_name[512];

if(strcmp(function_name[function_number],"TimerM$HandleFire")==0)

The above code gets compiled, but I get segmentation fault when I run
it. Can anyone suggest why ?

Thank you,

Priya
 
R

Roberto Waltman

priyanka said:
I am trying to compare the two strings but I get segmentation fault.
const char *function_name[512];
if(strcmp(function_name[function_number],"TimerM$HandleFire")==0)

The above code gets compiled, but I get segmentation fault when I run
it. Can anyone suggest why ?

You are not showing any code that can actually be compiled, so these
are only guesses:

a) What is 'function_number'? Is it an integer, or can it be
converted automatically by the compiler to an integer type?

b) If so, are you sure it has a value between 0 and 511 ?
(By the way, '512' should be replaced by a macro)

c) If so, has 'function_name[function_number]' been initialized
properly to point to a valid string?
(Hint: what does puts(function_name[function_number]); show?)


Roberto Waltman

[ Please reply to the group,
return address is invalid ]
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi there,

I am trying to compare the two strings but I get segmentation fault.

const char *function_name[512];

if(strcmp(function_name[function_number],"TimerM$HandleFire")==0)

The above code gets compiled, but I get segmentation fault when I run
it. Can anyone suggest why ?

Sure, but you've left out enough context that the suggestions may be
way off base.

1) function_number may be outside the range of 0 - 511

2) function_name[function_number] may not be initialized

3) you failed to link in the strcmp() library function

HTH
- --
Lew Pitcher

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

iD8DBQFEvR0VagVFX4UWr64RAkqCAJ4pxBEuwPNPyBnlvHV13RLUFij2ygCcCQpt
V5haty6GgG2h16fRJrWZHxw=
=kQ6F
-----END PGP SIGNATURE-----
 
A

Ancient_Hacker

priyanka said:
Hi there,

I am trying to compare the two strings but I get segmentation fault.

const char *function_name[512];

if(strcmp(function_name[function_number],"TimerM$HandleFire")==0)

The above code gets compiled, but I get segmentation fault when I run
it. Can anyone suggest why ?

Thank you,

Priya

have you ever set fucntion_name to point to a valid place?

What's the current value in function_number?

Do you think this is an array of 512 characters, or 512 strings?
 
M

Morris Dovey

priyanka (in (e-mail address removed))
said:

| Hi there,
|
| I am trying to compare the two strings but I get segmentation fault.
|
| const char *function_name[512];
|
| if(strcmp(function_name[function_number],"TimerM$HandleFire")==0)
|
| The above code gets compiled, but I get segmentation fault when I
| run it. Can anyone suggest why ?

Priya...

You have provided an array of (char *) but the pointers appear not to
point to anything!

You'll probably want to the declaration to something like:

const char *function_name[] = { "fn1","fn2", /*..*/ , "fnz" };
 
B

Ben Pfaff

priyanka said:
const char *function_name[512];

if(strcmp(function_name[function_number],"TimerM$HandleFire")==0)

The above code gets compiled, but I get segmentation fault when I run
it. Can anyone suggest why ?

Did you set function_name[function_number] to point to a string?
 
E

Eric Sosman

priyanka wrote On 07/18/06 13:28,:
Hi there,

I am trying to compare the two strings but I get segmentation fault.

const char *function_name[512];

if(strcmp(function_name[function_number],"TimerM$HandleFire")==0)

The above code gets compiled, but I get segmentation fault when I run
it. Can anyone suggest why ?

Probably because function_name[function_number] is
not a valid pointer value. What is the value of
function_pointer? What value did your program previously
store in function_name[function_number]?
 
K

Keith Thompson

Morris Dovey said:
priyanka (in (e-mail address removed))
said:
| I am trying to compare the two strings but I get segmentation fault.
|
| const char *function_name[512];
|
| if(strcmp(function_name[function_number],"TimerM$HandleFire")==0)
|
| The above code gets compiled, but I get segmentation fault when I
| run it. Can anyone suggest why ?

Priya...

You have provided an array of (char *) but the pointers appear not to
point to anything!

You'll probably want to the declaration to something like:

const char *function_name[] = { "fn1","fn2", /*..*/ , "fnz" };

We have no possible way of guessing whether the pointers point to
anything or not, since the OP didn't show us enough actual code.
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top