C pointer

I am reading the book “Learn C” right now and have run into a question that I really don't understand. The goal of the exercise is to find a problem with this code:

char c; c = 'a'; printf("c holds the character %c.",c); 

.. and then he gives an explanation that: "The text string" a "consists of two characters:" a "and a terminating null byte. The variable c is only one byte in size. Even if c were 2 bytes in length, you don’t could copy the text string this way. Try copying the text one byte at a time into a variable large enough to hold the text string and its terminating null byte. "

However, when I run the code above - it works fine. I thought I understood the theory why this is bad - the whole end is 0 at the end of the line, so I rewrote code like this to check:

 char c[2]; *c = 'a'; printf("c holds the character %c.",c); 

But this creates a problem. I'm starting to get confused with the problem. Wouldn't this second set of code pass the letter “a” to the pointer in c [0], and then put the completion 0 in c [1] - making full use of the 2 spaces allocated for this array?

+4
source share
9 answers

The text is incorrect or you are transcribing the code incorrectly.

In C, a double-quoted string has zero termination. One quotation mark is just one character.

+8
source

Explanation that: "The text string" a "consists of two characters:" a "and the terminating null byte.

This is true, but 'a' not "a" . 'a' is one character. Therefore 'a' very suitable for char .

An example of writing one char on one per line should look like this:

 char str[2]; str[0] = 'a'; str[1] = 0; /* because nothing guarantees array items are initialized to 0 */ printf("str holds the string %s.", str); 
+9
source

In C, single quotation marks indicate a constant character.

+2
source

In your second code snippet, c interpreted as a pointer to the first element of the array (i.e., as char * ), and not as char . You can either do:

 printf("c holds the character %c.", *c); 

or

 printf("c holds the character %c.", c[0]); 

or

 printf("c holds the character %s.", c); 

In the last example, %s tells printf() expect a string, not a character. However, in this case, you must set the second element c[] as a "null terminator" (using, for example, c[1] = '\0' ), otherwise printf() does not know where the line ends and will potentially print an infinite amount garbage.

+2
source

Strings in C are actually arrays of characters. So, char c [2]; this is a declaration for c, which is an array of characters with 2 elements. The purpose of "a" is incorrect, since it is a character literal, you must assign it to "a", which is a string literal. Just remember the difference between single quotes and double quotes, as indicated in previous comments.

+1
source

In C, 'c' is a single character, while "c" is a character string. (Note the double quotes). The author’s explanation of the book on a variable occupying 2 bytes corresponds only to the second case.

In your first code segment, you assign an absolutely valid 1-byte char variable to the char variable and print it. So it works.

In your second code segment, you assign the char to char array (if you want to call it a string), rather than null-terminate it. When you call the printf function on it, printf does not know where the line ends. So the behavior of printf is undefined here.

0
source

Something is wrong with the explanation shown in the book. "A" is a single char (note the single quotes), and "a" is a string. Now "a" really ends with 0x00. But "a" is not.

My conclusion at the moment is that either something was distorted when you copied the information from the book ... or ... the name of the author "Learn C" was the author’s goal.

0
source

In C single quotes, a single character is described, that is, 'a' while in double quotes "a" it makes up a zero titled string

So this is

 int main(void) { char s[2]="a"; // s='a'; leads to error incompatible types......... printf("%s",s); return 0; } 

will work fine

0
source

In C programming, the single-quoted character: 'a' and the double-quoted character: "a" have different meanings.

By placing one character in single quotation marks, you indicate that you want "a" to be interpreted as a single character, so the trailing null byte is not added.

If you must put your character in double quotation marks, it will be declared as a string. In this case, the terminating null byte is automatically concatenated to the end, and you do not need to worry. As a result, the line:

"a/0"

If you want to initialize your str variable in the string "a" using only single quotes, then you will need to explicitly add a terminating null byte.

 char str[2]; str[0] = 'a'; str[1] = '/0'; printf("str holds the string %s.", str); 

Otherwise, you can simply initialize the first str index to "a" using double quotes, and the terminating null byte will be automatically added to the second index.

 char str[2]; str[0] = "a"; 
0
source

All Articles