每次我尝试将字符串写入动态分配的字符串数组时,我都会收到 4 条"传递 strcpy 的参数 1 从整数制作指针而不进行强制转换"错误消息。我知道这显然与我的 strcpy 调用有关,并且这是某个地方的类型不匹配问题,但我需要一点帮助。
/* ---- LIBRARIES ---- */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* ---- PROTOTYPES ---- */
int readFx(char** arr);
/* int sortFx(char** arr, int arg2);
int printFx(char** arr, int arg2); */
int freeFx(char** arr, int cnt);
char* getToken(char arr1[], int loc);
void makeRoom(char*** t, int size);
/* ---- MAIN ---- */
int main(void)
{
char** pntrArr;
char* fileText;
int iniArrSize = 10;
int recCnt = 0;
/* array to store addresses of arrays forming the rows */
pntrArr = malloc(iniArrSize * sizeof(char*));
recCnt = readFx(pntrArr);
sortFx(pntrArr, recCnt);
/* printFx(pntrArr, recCnt); */
freeFx(pntrArr, recCnt);
return;
}
/* ---- FUNCTIONS ---- */
int readFx(char** arr)
{
/*
input: csv file of string arrays
output: count of records received
purpose: read file, store values in array and populate pointer array
*/
char buffer[350];
char temp[350];
char*** reallocTemp;
char* token;
int counter, index;
int subLoc = 3;
int enrLoc = 8;
int arrSize = 10;
/* Clear headers */
fgets(buffer, sizeof(buffer), stdin);
counter = 0;
/* While file stream is not null */
while (fgets(buffer, sizeof(buffer), stdin) != NULL)
{
/* Populate array within array if pntr arr has room */
if(counter <= arrSize)
{
/* buffer copy*/
strcpy(temp, buffer);
index = 0;
/* create array for token values */
arr[counter] = malloc(10 * sizeof(char));
/* Get first token */
token = getToken(temp, subLoc);
strcpy(arr[counter][index],token);
index++;
/* Get second token */
token = getToken(temp, enrLoc);
strcpy(arr[counter][index], token);
counter++;
}
else
{
/* Reallocate memory due to necessary expansion */
makeRoom(&arr, arrSize);
/* Realloc was successful */
if(temp != NULL)
{
arrSize = arrSize * 2;
/* Print Reallocation info */
printf("reallocating to %d", arrSize);
/* Populate values for current buffer now that you have realloc'd */
/* buffer copy*/
strcpy(temp, buffer);
index = 0;
/* create array for token values */
arr[counter] = malloc(10 * sizeof(char));
/* Get first token */
token = getToken(temp, enrLoc);
strcpy(arr[counter][index], token);
index++;
/* Get second token */
token = getToken(temp, subLoc);
strcpy(arr[counter][index], token);
counter++;
}
else
{
printf("unable to reallocaten");
exit(1);
}
}
return counter;
}
char* getToken(char arr1[], int loc)
{
/*
input: string array & location of desired string
output: string of token at position
purpose: grab string (char*) of certain position in given array
*/
int loopCnt;
char* del = ",n";
/* Grab first token */
char* token = strtok(buffer, del);
/* Loop through array to grab value at given location */
for(loopCnt = 1; loopCnt < loc; loopCnt++)
{
token = strtok(NULL, del);
}
return token;
}
int freeFx(char** arr, int cnt)
{
int i;
for(i = 0; i < cnt; i++)
{
free(arr[i]);
}
free( arr );
return 0;
}
void makeRoom(char*** t, int size)
{
*t = realloc(*t, size * 2 * sizeof(char*));
}
从代码:
strcpy(arr[counter][index],token);
arr[counter][index] 是指数组的单个字符,所以请使用 arr[counter] 而不是那个(它是指向动态分配块的指针),
以便您可以将字符串复制到该动态数组。
添加额外的细节,如果你使用strcpy,如下所示,
strcpy(arr[counter][index],token);
复制仍然完成,但不在正确的位置,
假设 arr[counter] 保存一个 char 指针的地址为 1000。
所以strcpy(arr[counter][index],token);将复制地址1000+索引中的字符串。
strcpy(arr[counter],token) 将复制地址 1000 中的字符串
希望这是有帮助的。