C修改全局字符数组

时间:2018-01-31 09:14:23

标签: c

我在C中遇到问题 我想在function1中更新/修改全局字符串(char数组)

如何更新我的变量dest并与main函数共享?

编辑: 我找到了解决方案

char *alphabet;

int function1(char* newstring) {


    alphabet = (char*)malloc(63);

    printf ("%s\n", alphabet);


    strcpy(alphabet, newstring);

    return 0;
}

int main() {
   char* test="newstring";
   function1(test);

   printf ("%s\n", alphabet);

    return 0;

3 个答案:

答案 0 :(得分:3)

让我们看一下gcc对您的代码的评价:

gcc -Wall test.c -o test

test.c: In function ‘function1’:
test.c:8:25: warning: comparison between pointer and integer
     while (newstring[i] != NULL) {
                         ^~
test.c:9:7: warning: implicit declaration of function ‘strcpy’ [-Wimplicit-function-declaration]
       strcpy(dest, newstring[i]);
       ^~~~~~
test.c:9:7: warning: incompatible implicit declaration of built-in function ‘strcpy’
test.c:9:7: note: include ‘<string.h>’ or provide a declaration of ‘strcpy’
test.c:9:20: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
       strcpy(dest, newstring[i]);
                    ^~~~~~~~~
test.c:9:20: note: expected ‘const char *’ but argument is of type ‘char’
test.c: In function ‘main’:
test.c:19:20: warning: comparison between pointer and integer
     while (dest[i] != NULL) {
                    ^~
test.c:20:22: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
       printf ("[%d] %s\n", i, dest[i]);
                      ^
test.c: At top level:
test.c:3:6: warning: array ‘dest’ assumed to have one element
 char dest[];
      ^~~~

正如你所看到的,有很多警告可以照顾。

  

认真对待警告并修复

这样做的代码可能是:

#include <stdio.h>

char dest[32];

int function1(char* newstring) {
    // Or simply use strcpy instead of a loop... 
    int i = 0;
    while(newstring[i] != '\0')
    {
      dest[i] = newstring[i];
      ++i;
    }
    dest[i] = '\0';
    return 0;
}

int main() {
    char* test="newstring";
    function1(test);
    int i = 0;
    while (dest[i] != '\0') {
      printf ("[%d] %c\n", i, dest[i]);
      ++i;
    }
    return 0;
}

输出:

[0] n
[1] e
[2] w
[3] s
[4] t
[5] r
[6] i
[7] n
[8] g

注意您还应检查缓冲区溢出,即i始终小于32.为清楚起见,我省略了但请确保在代码完成之前添加。

答案 1 :(得分:0)

我认为您不需要使用循环来使用strcpy复制字符串。

#include<string.h>
#include<stdio.h>

char dest[50];

int main()
{
    char* test = "newstring";
    strcpy(dest, test);
    int i = 0;
    while(dest[i] != '\0')
    {
       printf("%d: %c\n",i, dest[i]);
       i++;
    }
    return 0;
}

答案 2 :(得分:-1)

我已经为您的代码提供了解决方案。

char dest[];

此语句发出警告:数组'dest'假定有一个元素。

#include <stdio.h>
#include <string.h>

char dest[];

int function1(char* newstring)
{
    int i = 0;
    char temp[50];
    while (newstring[i]) {
        temp[i]=newstring[i];
        i++;
    }
    strcpy(dest, temp);
    return 0;
}

int main() {
    char* test="newstring";
    function1(test);
    int i = 0;
    while (dest[i]) {
      printf ("[%d] %c\n", i, dest[i]);
      i++;
    }
    return 0;
}

输出:

[0] n
[1] e
[2] w
[3] s
[4] t
[5] r
[6] i
[7] n
[8] g