简体   繁体   English

将字符串指针数组传递给函数并读取字符串的值

[英]Passing array of string pointers to function and reading value of the strings

I have an assignment where I am required to pass an array of string pointers to a function, assign strings and then read back these strings.我有一个任务,我需要将一个字符串指针数组传递给一个函数,分配字符串,然后读回这些字符串。 Here is what I am doing:这是我在做什么:

void getStr(char *str[])
{
  char temp[256];

  strcpy (temp,"Apple");
  *str = temp;
  printf("\ngetStr Str= %s",*str);
  str++;

  strcpy (temp,"Mango");
  *str = temp;
  printf("\ngetStr Str= %s",*str);
  str++;
}
int main()
{
  char *str[2] ;
  int i=0;
  getStr (str);
  for(i =0 ;i<2;i++)
    printf("\nstr addr =%x, str= %s\n",&str[i],str[i]);
   return 1 ;
}

    Here is my output:
       getStr Str= Apple
       getStr Str= Mango
       str addr =28d623b0, str=

       str addr =28d623b8, str=

So str gets the strings assigned correctly in getStr(), but when I print them in main() it is blank.所以str在getStr()中正确分配了字符串,但是当我在main()中打印它们时它是空白的。 What am I doing wrong here?我在这里做错了什么?

You stored into str the address of the first element of the local variable temp (the array decayed into a pointer), but that array's lifetime ends once getStr() returns.您将局部变量temp (数组衰减为指针)的第一个元素的地址存储到str ,但是一旦getStr()返回,该数组的生命周期就会结束。 Hence you are accessing a local variable after its lifetime ended, and undefined behavior results.因此,您在其生命周期结束后访问局部变量,并导致未定义的行为。

This sort question seems to be coming up constantly.这样的问题似乎不断出现。

void getStr(char *str[])
{
    char temp[256];
    ...
}

That temp array is a block of memory whose lifetime is tied to the duration of the getStr function.temp数组是一块内存,其生命周期与getStr函数的持续时间相关。

Also, when you overwrite it from "Apple" to "Mango", then the pointer you had stored that used to point to temp in str[0] will point to "Mango".此外,当您将其从“Apple”覆盖到“Mango”时,您存储的用于指向 str[0] 中的temp的指针将指向“Mango”。 str[0] and str[1] hold the same pointer! str[0] 和 str[1] 持有相同的指针! And that pointer is no longer pointing to valid memory as soon as you return from the function.一旦您从函数返回,该指针就不再指向有效内存。 :-/ :-/

For these kinds of problems, C programmers use things like strdup .对于这类问题,C 程序员使用strdup 之类的东西。 That does a memory allocation for each string and is a little bit more ergonomic than having to do strlen/malloc/strcpy.这会为每个字符串分配内存,并且比必须执行 strlen/malloc/strcpy 更符合人体工程学。 Either way, remember to free() each string when you're done with them.无论哪种方式,请记住在处理free()每个字符串后free()

This is very old post, but I must update it because I have similar problem, and I found out, that in side of function, I need use pointer only once, and array is restored like I am using this original from outside.这是很老的帖子,但我必须更新它,因为我有类似的问题,我发现,在函数的一侧,我只需要使用一次指针,并且数组被恢复,就像我从外面使用这个原始的一样。

void getStr(char *str)
{
   str[1] = (temp,"Apple");
   str[2] = (temp,"Mango");
}
int main()
{
  char str[2] ;
  int i=0;
  getStr (str);
  for(i =0 ;i<2;i++)
    printf(str[i]);
   return 1 ;
}

That's why I hate use pointers, is to much mist-confusion and pointers it self are used in wrong way.这就是为什么我讨厌使用指针的原因,因为它本身就是以错误的方式使用指针。 They should be used only in special cases, like passing some arrays to function, to prevent making copy of this same twice.它们应该只在特殊情况下使用,比如将一些数组传递给函数,以防止两次复制相同的数组。 And Function it self don't need output set of values.而函数本身不需要输出值集。 But that's is only in special case.但这只是在特殊情况下。 Using pointers on each line in program don't have sense if compiler is doing it anyway, From what I learned, pointers was design only because there was to much problems with functions and passing big sort of data.如果编译器无论如何都在做,那么在程序的每一行上使用指针是没有意义的,据我所知,指针只是设计,因为函数和传递大量数据存在很多问题。

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

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