Reputation: 175
What's wrong with this?
int main(int argc, char** argv) {
printf("%% ");
size_t len;
ssize_t read;
char* line;
int size;
read = getline(&line, &len,stdin);
printf("my name: %s\n",argv[0]);
printf("%s",line);
char* args[]= {"yoyoyo","hi","me",NULL};
return 0;
}
Debugging shows Exception: EXC_BAD_ACCESS (code=1, address=0xa66647360)) on the
printf("my name: %s\n",argv[0]); line.
Upvotes: 1
Views: 680
Reputation: 8492
I suspect your problem is not with that line but with this:
read = getline(&line, &len,stdin);
line
is declared as a char *
and isn't pointed to anything. Thus, you're reading in data to the memory location of an uninitialized pointer, which is why you're getting an access error.
Try either statically allocating line:
char line[256]; // or whatever line length you want
read = getline(&line, &len, stdin);
or using malloc:
char *line = malloc(sizeof(char) * 256);
read = getline(line, &len, stdin);
but be careful of overflowing whatever length you set. Good luck!
Upvotes: 0
Reputation: 39386
You're forgetting to initialize the values supplied to getline()
.
Try with char *line = NULL;
and size_t len = 0;
instead.
The man 3 getline
man page has an example you could adapt.
Upvotes: 1