我使用這個例子:複製字符串指針,指針
char *myData[][2] =
{{"John", "[email protected]"},
{"Erik", "[email protected]"},
{"Peter","[email protected]"},
{"Rikard","[email protected]"},
{"Anders","[email protected]"}};
char **tableData[6];
tableData[0] = myData[0];
tableData[1] = myData[1];
tableData[2] = myData[2];
tableData[3] = myData[3];
tableData[4] = myData[4];
tableData[5] = NULL;//null terminated array
,而是希望把我自己的字符串名稱和電子郵件。 (試圖將字符串xyz放入myData,然後tableData) strcpy與myData不會工作。我已經嘗試了指針和引用的所有組合,但它似乎沒有複製字符串。有什麼建議麼?
ok--> strncpy(xyz, argv[i], strlen(argv[i]));
ok--> strcpy(xyz + strlen(argv[i]), "\0");
run time stops here--> strncpy(myData[i][0], xyz, strlen(xyz));
tableData[i] = myData[i];
這不是問題,但你應該仔細看看strlen()和strncpy()是做什麼的。兩行: ok - > strncpy(xyz,argv [i],strlen(argv [i])); ok - > strcpy(xyz + strlen(argv [i]),「\ 0」); 沒有比 strcpy(xyz,argv [i])更安全(效率也更低)。 – Dipstick 2008-12-19 21:23:05