是否有可能转换的char []到char *用C? [英] Is it possible to convert char[] to char* in C?

查看:167
本文介绍了是否有可能转换的char []到char *用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个任务,我们必须从文件中读取一系列字符串到一个数组。我打电话给阵列的加密算法(密码调换二维数组)。所以,一开始我把从文件中的所有信息到一个二维数组,但我有很多相互矛盾的类型在我的code其余麻烦(具体试图设置的char []到char *)。所以,我决定改用数组的指针,在大多数我的code制成的一切轻松了许多。

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 [] )是的not~~V同样的事情。

It sounds like you're confused between pointers and arrays. Pointers and arrays (in this case char * and char []) are not the same thing.


  • 阵列烧焦一个[​​SIZE] 表示,在位置值 A 是长度的数组尺寸

  • 指针的char * A; 表示,在位置值 A 是一个指向一个字符。这可以用指针运算结合起来,表现得像一个数组(例如, A [10] 是过去无论 A 点)

  • An array char a[SIZE] says that the value at the location of a is an array of length SIZE
  • A pointer char *a; says that the value at the location of a is a pointer to a char. This can be combined with pointer arithmetic to behave like an array (eg, a[10] is 10 entries past wherever a points)

在内存中,它看起来像这样(例如,从常见问题):

In memory, it looks like this (example taken from the FAQ):

 char a[] = "hello";  // array

   +---+---+---+---+---+---+
a: | h | e | l | l | o |\0 |
   +---+---+---+---+---+---+

 char *p = "world"; // pointer

   +-----+     +---+---+---+---+---+---+
p: |  *======> | w | o | r | l | d |\0 |
   +-----+     +---+---+---+---+---+---+

这很容易混淆了指针和数组之间的区别,因为在很多情况下,一个数组引用衰变的指针到它的第一个元素。这意味着,在许多情况下,(当传递给函数调用如)阵列成为指针。如果您想了解更多,请的C常见问题本节详细介绍的差异。

一个重大现实不同的是,编译器知道数组有多长。使用上述的例子:

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 '\0' 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)

在没有看到您的code,这是很难推荐最佳的行动过程中,但我怀疑更改为使用指针到处都将解决你目前有问题的。注意,现在:

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的一个​​[10]; 将成为的char * A =的malloc(10 * sizeof的(炭)); ,其次是检查 A!= NULL 。请注意,您实际上并不需要说的sizeof(char)的在这种情况下,因为的sizeof(char)的是定义为1。我离开它的完整性。

  • You will need to initialise memory wherever the arrays used to be. Eg, char a[10]; will become char *a = malloc(10 * sizeof(char));, followed by a check that a != NULL. Note that you don't actually need to say sizeof(char) in this case, because sizeof(char) is defined to be 1. I left it in for completeness.

任何你previously了的sizeof(A)为数组的长度将需要由您分配的内存的长度来代替(如果你'重新使用字符串,你可以使用的strlen(),它计算到'\\ 0')。

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 '\0').

您将需要做出相应调用 免费() 每次调用的malloc()。这告诉你正在使用你要求与 malloc的内存完成计算机()。如果你的指针 A ,只写免费(一); 在该code一个地步,你知道你不再需要任何 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 成为元素的地址 [0] A

You can read this as "char pointer p becomes the address of element [0] of a".

这篇关于是否有可能转换的char []到char *用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆