help

R

Robert Smith

I've written this small program to extract names from a list. It does what
it it supposed to do, but then crashes (segmentation fault) at the very end.
Can anyone tell me why?

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

int main() {
char *test = "file1.jpg\nfile2.jpg\nfile3.jpg\nfilex.tga\n";
char *p = test;
char temp[13];
int i = 0; // index
memset(temp,0,13);
while (p) {
while (*p != '\n') {
temp = *p++;
i++;
}
puts(temp);
i=0;
p++; // bump over newline
}
}
 
I

Ico

Robert Smith said:
I've written this small program to extract names from a list. It does what
it it supposed to do, but then crashes (segmentation fault) at the very end.
Can anyone tell me why?

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

You forgot to include string.h here, you need it for the memset()
int main() {
char *test = "file1.jpg\nfile2.jpg\nfile3.jpg\nfilex.tga\n";
char *p = test;
char temp[13];
int i = 0; // index
memset(temp,0,13);
while (p) {

You probably mean 'while (*p)' here. This fixes your crash.
while (*p != '\n') {
temp = *p++;
i++;
}
puts(temp);
i=0;
p++; // bump over newline
}


When typing 'int main()', you promised you would return an integer here.
 
R

Robert Smith

Ico said:
Robert Smith said:
I've written this small program to extract names from a list. It does what
it it supposed to do, but then crashes (segmentation fault) at the very end.
Can anyone tell me why?

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

You forgot to include string.h here, you need it for the memset()
int main() {
char *test = "file1.jpg\nfile2.jpg\nfile3.jpg\nfilex.tga\n";
char *p = test;
char temp[13];
int i = 0; // index
memset(temp,0,13);
while (p) {

You probably mean 'while (*p)' here. This fixes your crash.
while (*p != '\n') {
temp = *p++;
i++;
}
puts(temp);
i=0;
p++; // bump over newline
}


When typing 'int main()', you promised you would return an integer here.


Thanks. That solved my problem.

Just also wondering, if you declare char *x = "some string";, will it
append a \0 at the end?
 
V

Vladimir S. Oka

Robert said:
Ico said:
Robert Smith said:
I've written this small program to extract names from a list. It does what
it it supposed to do, but then crashes (segmentation fault) at the very end.
Can anyone tell me why?

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

You forgot to include string.h here, you need it for the memset()
int main() {
char *test = "file1.jpg\nfile2.jpg\nfile3.jpg\nfilex.tga\n";
char *p = test;
char temp[13];
int i = 0; // index
memset(temp,0,13);
while (p) {

You probably mean 'while (*p)' here. This fixes your crash.
while (*p != '\n') {
temp = *p++;
i++;
}
puts(temp);
i=0;
p++; // bump over newline
}


When typing 'int main()', you promised you would return an integer here.


Thanks. That solved my problem.

Just also wondering, if you declare char *x = "some string";, will it
append a \0 at the end?


Yes, it will. Do note, however, that `x` is assigned a pointer to a
string literal "some string" which is not modifiable, and not reachable
in any other way. I.e., you can't do things like `x[0] = 'c'`, and
assigning a different pointer to `x` without saving the previous value
will make "some string" instance unraechable. See also <c-faq.com>, it
has good discussion on this (certainly better than what I just knocked
up).
 
H

Herbert Rosenau

I've written this small program to extract names from a list. It does what
it it supposed to do, but then crashes (segmentation fault) at the very end.
Can anyone tell me why?

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

int main() {
char *test = "file1.jpg\nfile2.jpg\nfile3.jpg\nfilex.tga\n";
char *p = test;
char temp[13];
int i = 0; // index
memset(temp,0,13);
while (p) {

Why does you test the pointer insted the char the pointer points to?
while (*p != '\n') {
temp = *p++;
i++;
}
puts(temp);
i=0;
p++; // bump over newline
}
}



--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 

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

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top