2
這是什麼意思:「不能收集變量」?我該如何解決它?它使調試器不會報告值。「不能收集變量」?
我想打電話給我的功能
write_argument2(argc, * argv, * string[0]);
我要去改變和重新排列argv
。我的變量string
是char **string[100][100];
,也許這並不理想。該字符串變量會用新的論據來更新argv
:
void write_argument2(int argc, char argv[], char *string[]) {
int j = 0;
for (j = 0; j < argc; j++) {
if (argv[j])
string[j] = strdup(&argv[j]);
}
}
但我做錯了什麼,它崩潰。錯誤表示「無法收集變量」和strdup
的段落錯誤。
我也試過它編譯,但也成爲一個分段錯誤如下:* string[j] = * strdup(& argv[j]);
GDB說:
GNU gdb (Ubuntu 7.11-0ubuntu1) 7.11
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./shell...done.
(gdb) run
Starting program: /home/dac/ClionProjects/shell2/openshell/shell
'PATH' is set to /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin.
$ ls
Program received signal SIGSEGV, Segmentation fault.
0x0000000000403b1a in write_argument2 (string=<optimized out>, argv=<optimized out>,
argc=<optimized out>) at main.c:147
147 string[j] = strdup(&argv[j]);
(gdb)
我應該使用strcpy
代替或更改一些聲明?
@Montao該分配是不對的,'的strlen(的strdup(的argv [J]))'是字符串'的argv [J]'的長度,它不是數量數組中的字符串。爲什麼不發佈調用函數的代碼? – fluter
'string = write_argument2(argc,argv);'是調用函數的代碼。 – Montao
@Montao看我的測試程序,它適用於我。 – fluter