[英]Cannot assign char to char pointer
Here is my code: 这是我的代码:
printf("%s\n", "test1");
char c = '2';
char * lines[2];
char * tmp1 = lines[0];
*tmp1 = c;
printf("%s\n", "test2");
I don't see the second printf
in console. 我在控制台中看不到第二个
printf
。
Question : Can anybody explain me what's wrong with my code? 问题 :有人可以解释我的代码有什么问题吗?
NOTE: I'm trying to learn C
:) 注意:我正在尝试学习
C
:)
This line: 这行:
char * lines[2];
declares an array of two char
pointers. 声明两个
char
指针的数组。 However, you don't actually initialize the pointers to anything. 但是,您实际上并没有初始化指向任何东西的指针。 So later when you do
*tmp1 = (char)c;
所以以后当你做
*tmp1 = (char)c;
then you assign the character c
to somewhere in memory, possibly even address zero (ie NULL
) which is a bad thing. 然后您将字符
c
分配到内存中的某个位置,甚至可能分配零地址(即NULL
),这是一件坏事。
The solution is to either create the array as an array of arrays, like 解决方案是将数组创建为数组数组,例如
char lines[2][30];
This declares lines to have two arrays of 30 characters each, and since strings needs a special terminator character you can have string of up to 29 characters in them. 这声明行包含两个由30个字符组成的数组,并且由于字符串需要特殊的终止符,因此您最多可以包含29个字符的字符串。
The second solution is to dynamically allocate memory for the strings: 第二种解决方案是为字符串动态分配内存:
char *lines[2];
lines[0] = malloc(30);
lines[1] = malloc(30);
Essentially this does the same as the above array-of-arrays declaration, but allocates the memory on the heap. 本质上,这与上面的array-of-arrays声明相同,但是在堆上分配了内存。
Of course, maybe you just wanted a single string of a single character (plus the terminator), then you were almost right, just remove the asterisk: 当然,也许您只想要一个包含单个字符的字符串(加上终止符),那么您几乎是正确的,只需删除星号即可:
char line[2]; /* Array of two characters, or a string of length one */
lines
is uninitialized, and tmp1
initialization is wrong. lines
未初始化,并且tmp1
初始化错误。
It should be: 它应该是:
char lines[2];
char * tmp1 = lines;
Alternatively, you can say: 或者,您可以说:
char * tmp1 = &lines[0];
Or else for an array of strings: 否则为字符串数组:
char lines[2][30];
char * tmp1 = lines[0];
The array lines
in uninitialized. 数组
lines
未初始化。 Thus lines[0]
is an uninitalized pointer. 因此,
lines[0]
是未初始化的指针。 Dereferencing it in *tmp1
is sheer undefined behaviour . 在
*tmp1
引用是纯粹的不确定行为 。
Here's an alternative, that may or may not correspond to what you want: 这是一种替代方法,可能与您想要的相对应:
char lines[2];
char * tmp1 = lines; // or "&lines[0]"
*tmp = c;
Or, more easily: 或者,更容易:
char lines[2] = { c, 0 };
The line 线
char * lines[2];
creates an array of two char pointers. 创建一个由两个char指针组成的数组。 But that doesn't allocate memory, it's just a "handle" or "name" for the pointer in memory.
但这并没有分配内存,它只是内存中指针的“句柄”或“名称”。 The pointer doesn't point to something useful.
指针没有指向有用的东西。
You will either have to allocate memory using malloc()
: 您要么必须使用
malloc()
分配内存:
char * lines = malloc(2);
or tell the compiler to allocate memory for you: 或告诉编译器为您分配内存:
char lines[2];
Note: Don't forget to terminate the string with a 0
byte before you use it. 注意:使用字符串之前,请不要忘记以
0
字节终止该字符串。
char *lines[2];
: A two element array of char pointers. :两个元素组成的char指针数组。
char *tmp;
: A pointer to a character. :指向字符的指针。
char *tmp = lines[0]
: The value inside the array element 0
of the lines
array is transferred into tmp
. char *tmp = lines[0]
:将lines
数组的数组元素0
内部的值传输到tmp
。 Because it is an automatic array, therefore it will have garbage as the value for lines[0]
. 因为它是一个自动数组,所以将有垃圾作为
lines[0]
的值。
*temp
: Dereference the garbage value. *temp
:取消引用垃圾值。 Undefined Behaviour. 未定义的行为。
char * tmp1 = lines[0];
here you declare a char pointer and initialize its pointer value to line[0],the fist element stored in line array which is also uninitialized. 在这里,您声明了一个char指针,并将其指针值初始化为line [0],这是第一个存储在line数组中的元素,该数组也未初始化。
now the tmp is pointing to somewhere unknown and not actually accessible. 现在,tmp指向未知且无法实际访问的地方。 When you
当你
*tmp1 = (char)c;
you are operating on a invalid memory address which causes a Segmentation fault. 您在无效的内存地址上操作,这会导致分段错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.