Trouble \ 0 null line termination (C)

I seem to be having problems with my line ending with \ 0. I'm not sure if this is the problem, so I decided to make a post.

First of all, I declared my lines as:

char *input2[5];

      

Later in the program, I added this line of code to convert any remaining unused slots to become \0

, changing all of them to become null terminators. Maybe with a for loop, but yes.

while (c != 4) {
    input2[c] = '\0';
    c++;
}

      

In Eclipse, when in debug mode, I see there are now 0x0s in the empty slots, not \ 0. Are these the same things? Another line where I declared it as

char input[15] = "";

      

shows \ 000 in debug mode.

My problem is that I am getting segmentation faults (on Debian VM ). However works on my Linux 12.04). My GUESS is that, since the line was not actually interrupted, the compiler doesn't know when it stops, and thus keeps trying to access the memory in the array when it is clearly not already bound.

Edit: I'll try to answer all the other questions shortly, but when I change the string declaration to another suggested one, my program crashes. There is a strtok () function used to chop up fgets input into strings and then put them into my input2 array.

So,

input1[0] = 'l'
input1[1] = 's'
input1[2] = '\n'

input2[0] = "ls". 

      

It is a shell simulator using fork and execvp. I will post more code soon.

Concerning the proposal:

char * input2 [5]; This is a perfectly legal expression, but it is defined by input2 as an array of pointers. To contain a string, it needs a char array.

I will try to change it again. I've tried this before, but I remember that it gave me another runtime error (seg fault?). I think this is because of the way I implemented the strtok () function. I will check it again. Thank!

EDIT 2: I added the answer below to update my progress. Thanks for the help!

Here.

...

+3


source to share


4 answers


You should look something like this:

char input2[5];

for (int c=0; c < 4; c++) {
    input2[c] = '\0';
}

      



0x0 and \ 0 are different representations of the same value 0;

+3


source


Answer 1:

Thanks for answers!

I made some changes from the answers, but I returned the char clause (or the correct string declaration) because, as someone pointed out, I have a strtok function. The string requires me to send a char *, so I went back to what I had originally (char * input [5]). I've posted my code up to strtok below. My problem is that the program works fine on my Ubuntu 12.04 but gives me a segfault error when I try to run it on a Debian VM.

I am rather confused as I originally thought the error was because the compiler was trying to access an array index that is not already associated. This doesn't seem like a problem because a lot of people have mentioned that 0x0 is another way to write \ 000. I've posted the variable section of the debug window below. Everything seems to be correct, though as far as I can tell .. hmm ..



enter image description here

Input2 [0] and input [0], input [1] - focus points.

Here is my code before the strtok function. The rest is just a fork and then the execvp call:

int flag = 0;
int i = 0;
int status;
char *s; //for strchr, strtok
char input[15] = "";
char *input2[5];
//char input2[5];

//Prompt
printf("Please enter prompt:\n");

//Reads in input
fgets(input, 100, stdin);

//Remove \n
int len = strlen(input);
if (len > 0 && input[len-1] == '\n')
    input[len-1] = ' ';

//At end of string (numb of args), add \0

//Check for & via strchr
s = strchr (input, '&');
if (s != NULL) { //If there is a &
    printf("'&' detected. Program not waiting.\n");
    //printf ("'&' Found at %s\n", s);
    flag = 1;
}


//Now for strtok
input2[i] = strtok(input, " "); //strtok: returns a pointer to the last token found in string, so must declare
                                //input2 as char * I believe

while(input2[i] != NULL)
{
    input2[++i] = strtok( NULL, " ");
}

if (flag == 1) {
    i = i - 1; //Removes & from total number of arguments
}

//Sets null terminator for unused slots. (Is this step necessary? Does the C compiler know when to stop?)
int c = i;
while (c < 5) {
    input2[c] = '\0';
    c++;
}

      

+3


source


Q: Why didn't you declare a string char input[5];

? Do you really need an extra layer of indirection?

Q: while (c < 4) is safer

. And don't forget to initialize "c"!

And yes, "0x0" in the debugger and "\ 0" in your source code are "the same thing".

SUGGESTED CHANGES:

char input2[5];
...
c = 0;
while (c < 4) {
    input2[c] = '\0';
    c++;
}

      

This will almost certainly fix the segmentation violation.

+1


source


char *input2[5];

      

This is a perfectly legal declaration, but it defines input2

as an array of pointers. To contain a string, it must be an array char

.

while (c != 4) {
    input2[c] = '\0';
    c++;
}

      

Again, this is legal, but since it input2

is an array of pointers, it input2[c]

is a pointer (of type char*

). The rules for null pointer constants are what '\0'

is a valid null pointer constant. The assignment is equivalent to:

input2[c] = NULL;

      

I don't know what you are trying to do with input2

. If you pass it to a function that expects a char*

string, your code won't compile - or at least you get a warning.

But if you want to input2

hold the string, it needs to be defined as:

char input2[5];

      

It's just unfortunate that the mistake you made turns out to be such that the C compiler doesn't necessarily diagnose. (There are too many different "zero" flavors in C, and they often replace each other.)

0


source







All Articles