简体   繁体   English

如何使用 malloc 分配字符串并使用 C 重新分配

[英]How to allocate a string using malloc and realloc using C

I am trying to (1) initialize a char array and (2) resize the array every time it reads a string.我正在尝试(1)初始化一个字符数组和(2)每次读取字符串时调整数组的大小。 However, whenever I try to compile, I see the message says that error: initializer element is not constant. char *ptr = malloc(1 * sizeof(*ptr));但是,每当我尝试编译时,我都会看到消息说error: initializer element is not constant. char *ptr = malloc(1 * sizeof(*ptr)); error: initializer element is not constant. char *ptr = malloc(1 * sizeof(*ptr));

I do not understand what's wrong with my codes.我不明白我的代码有什么问题。 I tried both (1) char *ptr = (char*) malloc(ptr * sizeof(char)) and (2) char *ptr = malloc(1 * sizeof(*ptr)) , but none of them worked.我尝试了 (1) char *ptr = (char*) malloc(ptr * sizeof(char))和 (2) char *ptr = malloc(1 * sizeof(*ptr)) ,但都没有奏效。

Here is my full codes:这是我的完整代码:

// char *ptr = (char*) malloc(ptr * sizeof(char));
char *ptr = malloc(1 * sizeof(*ptr));

void execute(char *splitInput)
{
  char myhistory[] = "myhistory";
  int string_length = strlen(splitInput);

  char *new_ptr = realloc(ptr, sizeof(char) * string_length);
 }

The problem is with the declaration char *ptr = malloc(1 * sizeof(*ptr));问题在于声明char *ptr = malloc(1 * sizeof(*ptr)); . . C doesn't allow code there, only static declarations. C 不允许那里的代码,只有 static 声明。 That is:那是:

char *ptr = "Foo bar baz.";

is fine, because it is assigning a static string.很好,因为它分配了一个 static 字符串。 There is no code to execute.没有要执行的代码。

The following will work:以下将起作用:

char *ptr;

int main(int argc, char *argv[]) {
  // char *ptr = (char*) malloc(ptr * sizeof(char));
  ptr = malloc(1 * sizeof(*ptr));
}

void execute(char *splitInput)
{
  char myhistory[] = "myhistory";
  int string_length = strlen(splitInput);

  char *new_ptr = realloc(ptr, sizeof(char) * string_length);
 }
char *ptr = (char*) malloc(ptr * sizeof(char));
char *ptr = malloc(1 * sizeof(*ptr));

In C, It is not permissible to execute code, such as the code in functions like malloc() , outside of any other function.在 C 中,不允许在任何其他 function 之外执行代码,例如malloc()等函数中的代码。 Thus, those statements will never work regardless of how you rearrange parts of it.因此,无论您如何重新排列其中的部分,这些语句都将永远不会起作用。

You need to declare the pointer at global scope and do the assignment to the pointer returned by malloc() inside of any function:您需要在全局 scope 处声明指针,并对任何 function 内部的malloc()返回的指针进行赋值:

#include <stdlib.h>
#include <string.h>

char *ptr;                              // declare ptr as pointer with global scope.

void execute(char *splitInput)
{
  //char myhistory[] = "myhistory";
  int string_length = strlen(splitInput);

  char *new_ptr = realloc(ptr, sizeof(char) * string_length);

  strcpy(ptr,splitInput);
}

int main()
{
   char s[] = "Hello World!";

   ptr = malloc(1 * sizeof(*ptr));       // assign ptr to point to the memory 
                                         // allocated by malloc. 
   execute(s);   
}

Note that myhistory has no use inside of the function execute() .请注意, myhistory在 function execute()内部没有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM