
線形リストを作ってその構造体の中にpathname配列に格納されている文字列を記憶させるプログラムを作っているのですが警告が出てきてうまく動作してくれません。"stack is success、"stack is not successの出力がされないのでそのあたりが間違っていると思います。全体ではないのですが該当箇所のプログラムを載せます。気になる部分はstrcpyの書式についてです。先頭アドレスを引数として渡すようなのですがこの書き方ではまずいですか?エラーを見ると配列pathnameの宣言が間違っていてポインタを用いるのかな?と思うのですがよくわかりません。わかる方がいたら教えていただけると幸いです。よろしくお願いします。
c
1コード 2typedef struct nodetag{ 3 char data[512]; 4 struct nodetag *next; 5} node_t; 6int stack(char,node_t *); 7char pathname[512]; 8node_t *p; 9 10 printf("Command is pushd\n"); 11 memset(pathname,'\0',512); 12 getcwd(pathname,512); 13 if (stack(pathname,p) == 0) { 14 printf("stack is success\n"); 15 }else{ 16 printf("stack is not success\n"); 17 } 18 19 20int stack(char pathname,node_t *p){ 21 node_t *nd; 22 nd = malloc(sizeof (node_t)); 23 if (nd == NULL) { 24 return 1; 25 }else{ 26 nd -> next = p; 27 strcpy(nd -> data,pathname); 28 p = nd; 29 //リストの先頭に追加 30 printf("nd -> data is %s\n",nd -> data ); 31 return 0; 32 }
線形リスト
コンパイル時に出てくるエラー
simple_shell.c:326:19: warning: incompatible pointer to integer conversion
passing 'char [512]' to parameter of type 'char' [-Wint-conversion]
if (stack(pathname,p) == 0) {
^~~~~~~~
simple_shell.c:31:15: note: passing argument to parameter here
int stack(char,node_t *);
^
simple_shell.c:396:25: warning: incompatible integer to pointer conversion
passing 'char' to parameter of type 'const char *'; take the address with
& [-Wint-conversion]
strcpy(nd -> data,pathname);
^~~~~~~~
&
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/secure/_string.h:83:33: note:
expanded from macro 'strcpy'
__builtin___strcpy_chk (dest, src, __darwin_obsz (dest))
^~~
回答2件
あなたの回答
tips
プレビュー