このコードは、vector(argv)を文字列に変換し、それを出力します。ただし、vect2strがライブラリ(my_vect2str)から呼び出されると、警告が表示されます。
warning: passing argument 1 of ‘puts’ makes pointer from integer without a cast
そして、実行されたときにsegfaults。ここでの関数vect2strは、ライブラリ(my_vect2str)の関数とまったく同じです。ライブラリは同じコンピュータでコンパイルされました。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../lib/my.h"
char *vect2str(char **str) {
if (str == NULL)
return NULL;
if (*str == NULL)
return NULL;
int num = 0;
char * a;
int i;
for(i = 0; str[i] != '\0'; )
num += strlen(str[i++]);
num += i;
a = (char *) xmalloc(num * sizeof(char));
//Make new string
char space = ' ';
char *end = "";
int j;
for(j = 0; str[j] != NULL; j++) {
strcat(a, str[j]);
strcat(a, &space);
}
strcat(a, end);
return a;
}
int main(int argc, char **argv) {
puts(vect2str(argv));
//This does not work
//puts(my_vect2str(argv));
}