将字符串写入动态分配的数组

时间:2016-03-11 03:51:45

标签: c arrays dynamic malloc strcpy

我一直得到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 reallocate\n");
                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*));
}

1 个答案:

答案 0 :(得分:0)

来自代码:

datastore = DatastoreOptions.builder()
.authCredentials(AuthCredentials.createApplicationDefaults())
.build()
.service();
String id = "asdf123";
Query<Entity> query = Query.gqlQueryBuilder(
    Query.ResultType.ENTITY, 
    "SELECT * FROM Task WHERE id = '"+id+"'")
    .allowLiteral(true)
    .build();
QueryResults<Entity> result = datastore.run(query);
while(result.hasNext()){
    Entity temp = result.next();
    //(...)
}

arr [counter] [index]是指数组的单个字符,所以请使用arr [counter]而不是(指向动态分配的块的指针),

以便您可以将字符串复制到该动态数组。

如果您使用strcpy,请添加额外的详细信息,

的strcpy(ARR [计数器] [指数],令牌);

副本仍然完成,但不在正确的位置,

假设arr [counter]保存char指针的地址,该指针为1000。

如此strcpy(arr [counter] [index],token);将复制地址1000 + index中的字符串。

strcpy(arr [counter],token)将复制地址1000

中的字符串

希望这有用。