Seg Fault

D

Digital Khaos

Hello all,

I've been watching the group now for a bit, and I'm still not sure
if this question is relelvent to this group, but here goes anyway:

The following code:

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

int main(void)
{
char *string = "//usr//bin//emacs";

strcat(string, " &");
system(string);

return(0);
}

is throwing a seg fault. Can anyone tell me why?

Sorry if this is off topic.
 
E

E. Robert Tisdale

Digital said:
Hello all,

I've been watching the group now for a bit, and I'm still not sure
if this question is relevant to this group, but here goes anyway:

The following code:

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
char string[20] = "//usr//bin//emacs";

strcat(string, " &");
system(string);

return 0;
}

is throwing a seg fault. Can anyone tell me why?

Note my revision above.
You need to reserve enough space in your string array
to append two characters.
 
M

Martin Dickopp

Digital Khaos said:
The following code:

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

int main(void)
{
char *string = "//usr//bin//emacs";

strcat(string, " &");
system(string);

return(0);
}

is throwing a seg fault. Can anyone tell me why?

`string' is pointing to a string literal. You cannot modify a string literal
or append anything to it. The `strcat' function expects that its first
argument points to a buffer large enough to hold resulting string. You
must provide such a buffer. This is one way to do it:


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

int main (void)
{
const char *const command = "/usr/bin/emacs";
const char *const bg = " &";
char *buffer;

/* `buffer' should be large enough to hold the concatenated
string, including the terminating '\0' character. */
buffer = malloc (strlen (command) + strlen (bg) + 1);

if (buffer != NULL)
{
strcpy (buffer, command);
strcat (buffer, bg);
system (buffer);
free (buffer);
}
else
{
fputs ("memory allocation failed\n", stderr);
return EXIT_FAILURE;
}

return 0;
}


Martin
 
D

Default User

Digital said:
Hello all,

I've been watching the group now for a bit, and I'm still not sure
if this question is relelvent to this group, but here goes anyway:

The following code:

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

int main(void)
{
char *string = "//usr//bin//emacs";

strcat(string, " &");
system(string);

return(0);
}

is throwing a seg fault. Can anyone tell me why?

Sorry if this is off topic.


1. You can't modify string literals.

2. There's no place to store the characters you want to add to the
string.

3. http://www.eskimo.com/~scs/C-faq/top.html



Brian Rodenborn
 
D

Digital Khaos

The following code:

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

int main(void)
{
char *string = "//usr//bin//emacs";

strcat(string, " &");
system(string);

return(0);
}

is throwing a seg fault. Can anyone tell me why?

Sorry if this is off topic.

Thanks, I totally see and understand my mistake, works now
appreciate it.
 
P

Peter Shaggy Haywood

Groovy hepcat Digital Khaos was jivin' on Thu, 14 Aug 2003 21:03:19
GMT in comp.lang.c.
Seg Fault's a cool scene! Dig it!
I've been watching the group now for a bit, and I'm still not sure
if this question is relelvent to this group, but here goes anyway:

The following code:

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

int main(void)
{
char *string = "//usr//bin//emacs";

The above string is apparently wrong. You seem to be confusing
forward slashes (/) with backslashes (\) which are used to introduce a
character escape sequence in a character constant or string literal.
Because of this, backslashes themselves need to be escaped in
character constants and string literals to produce literal backslash
characters, but forward slashes do not.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
K

Keith Thompson

Groovy hepcat Digital Khaos was jivin' on Thu, 14 Aug 2003 21:03:19
GMT in comp.lang.c.
Seg Fault's a cool scene! Dig it! [...]
char *string = "//usr//bin//emacs";

The above string is apparently wrong. You seem to be confusing
forward slashes (/) with backslashes (\) which are used to introduce a
character escape sequence in a character constant or string literal.
Because of this, backslashes themselves need to be escaped in
character constants and string literals to produce literal backslash
characters, but forward slashes do not.

<OT>
But it's likely to work anyway. On systems with Unix-like file
systems, "//usr//bin//emacs" is typically equivalent to
"/usr/bin/emacs".
</OT>
 

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

seg fault 11
seg fault 10
A seg fault 11
Eliminating this seg fault 5
seg fault 76
Seg-fault in recursion 5
PyGILState_Release produces a seg fault 0
newbie question seg fault printf 8

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top