问题描述
我正在做一个任务,我们必须将一系列字符串从一个文件读入一个数组.我必须在数组上调用密码算法(密码转置二维数组).因此,起初我将文件中的所有信息放入一个二维数组中,但我在其余代码中遇到了很多冲突类型的问题(特别是尝试将 char[] 设置为 char*).所以,我决定切换到一个指针数组,这使得我的大部分代码中的一切都变得更加容易.
I'm doing an assignment where we have to read a series of strings from a file into an array. I have to call a cipher algorithm on the array (cipher transposes 2D arrays). So, at first I put all the information from the file into a 2D array, but I had a lot of trouble with conflicting types in the rest of my code (specifically trying to set char[] to char*). So, I decided to switch to an array of pointers, which made everything a lot easier in most of my code.
但现在我需要将 char* 转换为 char[] 并再次转换回来,但我无法弄清楚.我一直无法在谷歌上找到任何东西.我开始怀疑这是否可能.
But now I need to convert char* to char[] and back again, but I can't figure it out. I haven't been able to find anything on google. I'm starting to wonder if it's even possible.
推荐答案
听起来你对指针和数组感到困惑.指针和数组(在本例中为 char *
和 char []
)是 不是一回事.
It sounds like you're confused between pointers and arrays. Pointers and arrays (in this case char *
and char []
) are not the same thing.
- 一个数组
char a[SIZE]
表示a
位置的值是一个长度为SIZE
的数组 - 指针
char *a;
表示a
位置的值是指向char
的指针.这可以与指针算法相结合,使其表现得像一个数组(例如,a[10]
是a
指向的 10 个条目)
- An array
char a[SIZE]
says that the value at the location ofa
is an array of lengthSIZE
- A pointer
char *a;
says that the value at the location ofa
is a pointer to achar
. This can be combined with pointer arithmetic to behave like an array (eg,a[10]
is 10 entries past wherevera
points)
在记忆中,它看起来像这样(示例取自 常见问题解答):
In memory, it looks like this (example taken from the FAQ):
char a[] = "hello"; // array
+---+---+---+---+---+---+
a: | h | e | l | l | o | |
+---+---+---+---+---+---+
char *p = "world"; // pointer
+-----+ +---+---+---+---+---+---+
p: | *======> | w | o | r | l | d | |
+-----+ +---+---+---+---+---+---+
指针和数组之间的区别很容易混淆,因为在许多情况下,数组引用衰减"到指向它的第一个元素的指针.这意味着在许多情况下(例如传递给函数调用时)数组成为指针.如果您想了解更多信息,C 常见问题解答的这一部分详细描述了不同之处.
It's easy to be confused about the difference between pointers and arrays, because in many cases, an array reference "decays" to a pointer to it's first element. This means that in many cases (such as when passed to a function call) arrays become pointers. If you'd like to know more, this section of the C FAQ describes the differences in detail.
一个主要的实际区别是编译器知道数组的长度.使用上面的例子:
One major practical difference is that the compiler knows how long an array is. Using the examples above:
char a[] = "hello";
char *p = "world";
sizeof(a); // 6 - one byte for each character in the string,
// one for the ' ' terminator
sizeof(p); // whatever the size of the pointer is
// probably 4 or 8 on most machines (depending on whether it's a
// 32 or 64 bit machine)
如果没有看到您的代码,很难推荐最佳行动方案,但我怀疑更改为在任何地方使用指针将解决您当前遇到的问题.请注意:
Without seeing your code, it's hard to recommend the best course of action, but I suspect changing to use pointers everywhere will solve the problems you're currently having. Take note that now:
你需要在数组曾经所在的地方初始化内存.例如,
char a[10];
将变为char *a = malloc(10 * sizeof(char));
,然后检查a !=空
.请注意,在这种情况下,您实际上不需要说sizeof(char)
,因为sizeof(char)
被定义为 1.为了完整起见,我将其保留.
You will need to initialise memory wherever the arrays used to be. Eg,
char a[10];
will becomechar *a = malloc(10 * sizeof(char));
, followed by a check thata != NULL
. Note that you don't actually need to saysizeof(char)
in this case, becausesizeof(char)
is defined to be 1. I left it in for completeness.
您以前使用 sizeof(a)
表示数组长度的任何地方都需要替换为您分配的内存长度(如果您使用的是字符串,则可以使用 strlen()
,直到 ' '
).
Anywhere you previously had sizeof(a)
for array length will need to be replaced by the length of the memory you allocated (if you're using strings, you could use strlen()
, which counts up to the ' '
).
您需要对 free()进行相应的调用code>
用于每次调用 malloc()
.这告诉计算机您已完成使用 malloc()
请求的内存.如果您的指针是 a
,只需在代码中您知道不再需要任何 a
点的位置写入 free(a);
到.
You will need a make a corresponding call to free()
for each call to malloc()
. This tells the computer you are done using the memory you asked for with malloc()
. If your pointer is a
, just write free(a);
at a point in the code where you know you no longer need whatever a
points to.
正如另一个答案所指出的,如果你想获取一个数组的起始地址,你可以使用:
As another answer pointed out, if you want to get the address of the start of an array, you can use:
char* p = &a[0]
可以理解为字符指针p
变成a
的元素[0]
的地址".
You can read this as "char pointer p
becomes the address of element [0]
of a
".
这篇关于是否可以在 C 中将 char[] 转换为 char*?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!