2d char数组中C指针的问题

时间:2021-09-03 21:34:33

Why does this work:

为什么这样做:

    //split returns (char**)
    char *temp2;
    temp2=split(array[i],'=')[1];

and this doesn't:

这不是:

char **temps;
temps[0]=temp2; //crashes

or this:

temps[0]=split(array[i],'=')[1]; //crashes

8 个解决方案

#1


2  

temps is just a pointer to a char*, but it has no initalized, sensible value! temps[0] is equivalent to *(temps + 0), but you cannot dereference a garbage value -- you first have to make temps points somewhere useful, e.g. by allocating memory for it.

temps只是指向char *的指针,但它没有初始化,明智的价值! temps [0]相当于*(temps + 0),但是你不能取消引用一个垃圾值 - 首先你必须在某个有用的位置设置temps点,例如通过为它分配内存。

If you want to store some char*s with automatic storage, then declare an array of char pointers instead:

如果你想存储一些带有自动存储的char *,那么请声明一个char指针数组:

char * temps[20];
temps[0] = /*... etc. ...*/

#2


2  

Split returns a pointer to a pointer to a char. So, what is returned from split(array[i],'=')[1] is a pointer to char which is what you declared on the stack and thus reserved space for it. That is why it works.

Split返回指向char的指针。因此,从split返回的内容(array [i],'=')[1]是指向char的指针,这是您在堆栈上声明的内容,因此为它保留了空间。这就是为什么它有效。

The other two don't work because the space pointed to is not allocated. You should use malloc().

其他两个不起作用,因为指向的空间未分配。你应该使用malloc()。

#3


0  

If you simply declare a pointer variable:

如果只是声明一个指针变量:

char **temps;

It's not really pointing to somewhere. Well, it actually is, but that's probably garbage (anywhere in memory). You have to initialize it before using it. Try allocating space for it.

它并没有真正指向某个地方。嗯,它实际上是,但那可能是垃圾(在内存中的任何地方)。您必须在使用它之前对其进行初始化。尝试为它分配空间。

#4


0  

char **temps was never allocated any space. You're dereferencing bad memory with temps[0].

char ** temps从未分配任何空间。你用temps [0]取消引用坏内存。

#5


0  

man malloc()

You have to allocate your memory to get space!

你必须分配你的记忆以获得空间!

#6


0  

char **temps;
temps[0]=temp2; //crashes

The pointer, temps, is uninitialized. Dereferencing it is an undefined operation.

指针temps未初始化。取消引用它是一个未定义的操作。

#7


0  

Because temps isn't initialized. It's a random value pointing to a random memory location.

因为temps没有初始化。它是指向随机存储器位置的随机值。

#8


0  

Think of it this way:

想一想:

char *temp2 = "foo";

char **temps;

temps[0] = temp2; // won't work
temps = &temp2; // ok

temp2 points at a C string. You can point temps at the address of temp2, (&temp2) but you can't dereference temps (that is, temps[0]) unless you first make it point at something valid. From your question it sounds like you want to malloc() an array of char* first.

temp2指向C字符串。你可以在temp2,(&temp2)的地址指向temps,但你不能取消引用temps(即temps [0]),除非你首先指出有效的东西。从你的问题来看,听起来你想要malloc()一个char *数组。

In the second and third cases, you are dereferencing temps[0] without first making it refer to some valid memory location. As has been pointed out, temps is pointing at a garbage location.

在第二种和第三种情况下,您将取消引用temps [0]而不首先引用某个有效的内存位置。正如已经指出的那样,temps指向垃圾位置。

Your first case works because you're dereferencing split(), so it's giving you a char*.

你的第一个案例是有效的,因为你正在取消引用split(),所以它给你一个char *。

#1


2  

temps is just a pointer to a char*, but it has no initalized, sensible value! temps[0] is equivalent to *(temps + 0), but you cannot dereference a garbage value -- you first have to make temps points somewhere useful, e.g. by allocating memory for it.

temps只是指向char *的指针,但它没有初始化,明智的价值! temps [0]相当于*(temps + 0),但是你不能取消引用一个垃圾值 - 首先你必须在某个有用的位置设置temps点,例如通过为它分配内存。

If you want to store some char*s with automatic storage, then declare an array of char pointers instead:

如果你想存储一些带有自动存储的char *,那么请声明一个char指针数组:

char * temps[20];
temps[0] = /*... etc. ...*/

#2


2  

Split returns a pointer to a pointer to a char. So, what is returned from split(array[i],'=')[1] is a pointer to char which is what you declared on the stack and thus reserved space for it. That is why it works.

Split返回指向char的指针。因此,从split返回的内容(array [i],'=')[1]是指向char的指针,这是您在堆栈上声明的内容,因此为它保留了空间。这就是为什么它有效。

The other two don't work because the space pointed to is not allocated. You should use malloc().

其他两个不起作用,因为指向的空间未分配。你应该使用malloc()。

#3


0  

If you simply declare a pointer variable:

如果只是声明一个指针变量:

char **temps;

It's not really pointing to somewhere. Well, it actually is, but that's probably garbage (anywhere in memory). You have to initialize it before using it. Try allocating space for it.

它并没有真正指向某个地方。嗯,它实际上是,但那可能是垃圾(在内存中的任何地方)。您必须在使用它之前对其进行初始化。尝试为它分配空间。

#4


0  

char **temps was never allocated any space. You're dereferencing bad memory with temps[0].

char ** temps从未分配任何空间。你用temps [0]取消引用坏内存。

#5


0  

man malloc()

You have to allocate your memory to get space!

你必须分配你的记忆以获得空间!

#6


0  

char **temps;
temps[0]=temp2; //crashes

The pointer, temps, is uninitialized. Dereferencing it is an undefined operation.

指针temps未初始化。取消引用它是一个未定义的操作。

#7


0  

Because temps isn't initialized. It's a random value pointing to a random memory location.

因为temps没有初始化。它是指向随机存储器位置的随机值。

#8


0  

Think of it this way:

想一想:

char *temp2 = "foo";

char **temps;

temps[0] = temp2; // won't work
temps = &temp2; // ok

temp2 points at a C string. You can point temps at the address of temp2, (&temp2) but you can't dereference temps (that is, temps[0]) unless you first make it point at something valid. From your question it sounds like you want to malloc() an array of char* first.

temp2指向C字符串。你可以在temp2,(&temp2)的地址指向temps,但你不能取消引用temps(即temps [0]),除非你首先指出有效的东西。从你的问题来看,听起来你想要malloc()一个char *数组。

In the second and third cases, you are dereferencing temps[0] without first making it refer to some valid memory location. As has been pointed out, temps is pointing at a garbage location.

在第二种和第三种情况下,您将取消引用temps [0]而不首先引用某个有效的内存位置。正如已经指出的那样,temps指向垃圾位置。

Your first case works because you're dereferencing split(), so it's giving you a char*.

你的第一个案例是有效的,因为你正在取消引用split(),所以它给你一个char *。