简体   繁体   中英

C programming pointers and char array problems

I want to pass the contents of an array to another method and have that method print out the entire array - how would i do this?

Currently:

I'm returning an array from a function.

    char* search_value(struct PDB *llist)
{
    int realID = -7; 
    int x = 0; 
    int task = 0; 
    char *received; 
    char theMessage[100]; 
    theMessage[0] = '\0';


    printf("Your choice: `Search'\n");
    printf("Enter the value you want to find: ");
    scanf("%d", &task);

    while(llist->data1 != NULL)
    {

        if(task == llist->taskID)
        {
            realID = llist->taskID; 
            strcpy(theMessage, llist->data1); 


            break; 
        }

    }

    return theMessage; 
}

i'm getting the return value:

 void getMessage(const int GET_MESSAGE)
{
    char * received = NULL;
    int x = 0; 
    received = search_value(llist);

    printf("%s", received); 

}

I want to somehow print the entire value (rather than just the first value to which the pointer is pointing at - how would i do this?

A few corrections and it should work:

// - struct contents shouldn't be changed by the function, make its pointer const.
// - pass a pointer to an allocated array as parameter
char* search_value(const struct PDB *llist, char* theMessage)
{
    int realID = -7;
    int x = 0;
    int task = 0;
    char *received;
    theMessage[0] = '\0';


    printf("Your choice: `Search'\n");
    printf("Enter the value you want to find: ");
    scanf("%d", &task);

    while(llist->data1 != NULL)
    {

        if(task == llist->taskID)
        {
            realID = llist->taskID;
            strcpy(theMessage, llist->data1);


            break;
        }

    }

    return theMessage;
}


void getMessage(const int GET_MESSAGE)
{
    char received[100]; // allocate the array outside the function
    int x = 0;
    search_value(llist, received); // pass a pointer to the first element

    printf("%s", received);

}

You have an issue with variable scope here: theMessage is local to the function search_value , so you're returning a pointer to an array which no longer exists once the function completes.

Instead you should use malloc() to allocate the space for theMessage and then subsequently free() it later on outside of the function when you're finished with it — however this can often lead to memory leaks if you're not diligent about cleaning up after yourself.

You can allocate the memory like so:

char * message = malloc(100);

One alternative would be to allocate the buffer in getMessage() and pass a pointer to the buffer into search_value which could then write into it:

void getMessage(const int GET_MESSAGE)
{
    char received[100];
    int x = 0; 
    search_value(llist, received);
    printf("%s", received); 
}

void search_value(struct PDB *llist, char * buffer)
{
    // write to buffer
}

Another option is to declare a char * pointer inside getMessage() , pass a pointer to a pointer into search_value() and again use malloc() to allocate space for the buffer.

Finally, this is a minor style criticism, but you'd do well to learn to stick to one convention for naming your functions, search_value and getMessage are not consistent names, and this will irk many a coder that you work with.

You have several problems with your code. I'm guessing that you want to search a list for some value, then return that value.

The first problem is that you do not actually iterate over the list, but only check the same item over and over again. The other problem is that you return a pointer to a local variable. This is undefined behavior, because as soon as the function returns the memory the pointer points to can be used for something else.

I suggest you change your code as follows:

char *search_value(struct PDB *llist, char *theMessage, size_t theMessageMaxLength)
{
    int realID = -7; 
    int task = 0; 

    printf("Your choice: `Search'\n");
    printf("Enter the value you want to find: ");
    scanf("%d", &task);

    while(llist != NULL && llist->data1 != NULL)
    {
        if(task == llist->taskID)
        {
            realID = llist->taskID; 
            strncpy(theMessage, llist->data1, theMessageMaxLength);
            theMessage[theMessageMaxLength] = '\0';
            break; 
        }
        llist = llist->next;  /* Assuming the field is named "next" */
    }

    return theMessage; 
}

void getMessage(const int GET_MESSAGE)
{
    char *received = NULL;
    char theMessage[100];

    /* Subtract 1 from the size, for the terminating '\0' */
    received = search_value(llist, theMessage, sizeof(theMessage) - 1);

    printf("%s", received); 
}

the array you are returning is local to that function. Either the calle function shall provide the array in which it expects the values or use static array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM