Why segmentation fault in this simple code?

P

Polar

Hi!

i'm a newbie in C language and i'm writing my first simple codes.

In one of these, my purpose is to append the ascii value of an interger
(example 101 --> e) at the end of a string to obtain a new (longer)
string. Example:

string: languag
letter: e
string (new value): language

In compiling, i received "Segmentation fault" error but i
can't understand why.

This is the very simple code:

main()
{
char* output="languag";
int i = 101;
char* letter;
letter[0]=(char)i;
letter[1]='\0';
printf("%s\n",letter); //prints "e"
//next line is not executed
strcat(output,letter); //segmentation fault printf("%s\n",output);

}

Thank you :)
 
P

Polar

main()
{
char* output="languag";
int i = 101;
char* letter;
letter[0]=(char)i;
letter[1]='\0';
printf("%s\n",letter); //prints "e"
//next line is not executed
strcat(output,letter); //segmentation fault printf("%s\n",output);

}

Sorry fo incorrect line-wrap.

This is the code:

main()
{
char* output="languag";
int i = 101;
char* letter;
letter[0]=(char)i;
letter[1]='\0';
printf("%s\n",letter); //prints "e"
//next line is not executed
strcat(output,letter); //segmentation fault
printf("%s\n",output);

}

Thank you :)
 
N

Nick Patavalis

Hi!

In compiling, i received "Segmentation fault" error but i
can't understand why.

Probably not "in compiling" but "in running", but anyway.
main()
{
char* output="languag";
int i = 101;
char* letter;
^^^^^^^

This is a pointer to a character. Currently it points to a random
address inside the computer's "memory". It points to an address that
possibly (and probably) does not "belong" to your "program". Or it can
point to an address where other usefull information is stored, like
the code of your program, or other variables of your program, or
anything.
letter[0]=(char)i;
letter[1]='\0';

Now you try to write something to this random address (that probably
doesn't even belong to you), as well as to the address next to it. A
segfault is imminent!

/npat
 
E

Emmanuel Delahaye

Polar wrote on 09/08/04 :
main()
{
char* output="languag";

string literals are not guaranteed to be writable. Better to define
them read-only:

char const * output="languag";
int i = 101;

What is this magic value supposed to represent? If you want 'e', just
use 'e'.
char* letter;

Warning. This variable is not initialized, meaning that its value is
undetermined. Being a pointer, it points anywhere.
letter[0]=(char)i;

Dereferencing an undefined pointer invokes an undefined behaviour (UB).
Your program is dead. Fix it.
letter[1]='\0';

UB again.
printf("%s\n",letter); //prints "e"

UB again. (Passing an undefined value to a function)
//next line is not executed
strcat(output,letter); //segmentation fault

UB again for many reasons:

- 'letter' is still undefined
- 'output' is not writable
- Should it be writable, there is no room enough for "e"
printf("%s\n",output);
}

You must use array oc ahrs to do what you want. Memory space doesn't
come by magic.
 
R

Ravi Uday

Polar said:
main()
{
char* output="languag";
int i = 101;
char* letter;
letter[0]=(char)i;
letter[1]='\0';
printf("%s\n",letter); //prints "e"
//next line is not executed
strcat(output,letter); //segmentation fault printf("%s\n",output);

}

Sorry fo incorrect line-wrap.

This is the code:

main()
{
char* output="languag";

'output' is readonly !!
int i = 101;
char* letter;
letter[0]=(char)i;

/* No need of a cast here*/
letter[1]='\0';
printf("%s\n",letter); //prints "e"
//next line is not executed
strcat(output,letter); //segmentation fault

Sure it is segmentation fault..Change the declaration to
char output[32]="languag"; /* Note 32 is a magic number (avoid it
whereever possible */
Ditto for char *letter; change to
char letter[32];
 
E

Emmanuel Delahaye

Ravi Uday wrote on 09/08/04 :
int i = 101;
char* letter;
letter[0]=(char)i;

/* No need of a cast here*/

Yes, but you missed the point. 'letter' is not initialized. The array
is no-existing. 'letter[0]' invokes a UB.
 
E

Emmanuel Delahaye

Grumble wrote on 09/08/04 :
I'd write inexistent (close to French spelling) or nonexistent ;-)

Yes, sorry. Thanks for correcting me. I know it's boring to read bad
English.
 
C

Christopher Benson-Manica

Polar said:

main() returns an int. The return type will not default to int under
C99, and it's a good idea to specify its return value even under C89.
Prefer the following:

int main( void )

Other people have already pointed out the actual error(s) in your
code.
 
E

Emmanuel Delahaye

(supersedes <[email protected]>)

Polar wrote on 09/08/04 :
main()
{
char* output="languag";

string literals are not guaranteed to be writable. Better to define
them read-only:

char const * output="languag";
int i = 101;

What is this magic value supposed to represent? If you want 'e', just
use 'e'.
char* letter;

Warning. This variable is not initialized, meaning that its value is
undetermined. Being a pointer, it points anywhere.
letter[0]=(char)i;

Dereferencing an undefined pointer invokes an undefined behaviour (UB).
Your program is dead. Fix it.
letter[1]='\0';

UB again.
printf("%s\n",letter); //prints "e"

UB again. (Passing an undefined value to a function)
//next line is not executed
strcat(output,letter); //segmentation fault

UB again for many reasons:

- 'letter' is still undefined
- 'output' is not writable
- Should it be writable, there is no room enough for "e"
printf("%s\n",output); }

You must use array of chars to do what you want. Memory space doesn't
come by magic.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top