1

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?

1
  • Check the c-faq Commented Dec 15, 2011 at 10:07

4 Answers 4

2

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);

}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

5 Comments

received = &theMessage[0]; return received; something like this you mean?
What makes you say that? The following code will do exactly what you would expect, allocates a buffer, writes to it, prints it, deletes it again. char * message = malloc(100); sprintf(message, "Hello, World!\n"); printf(message); free(message);
Also due to the way pointers and arrays are handled in C, with the exception of using externed variables, an array and a pointer to a type of that array are basically interchangeable, so &theMessage[0] is equivalent to theMessage in this instance.
i put that line in my code and it gave errors.... i did a copy paste, not sure why it's not working... thanks for help though.. i'll keep all your advice in mind...
You'd have to show those errors for help fixing that, still the other approach (which I put in more detail) is the safer one and the one you should probably use. You should also consider the possibly that whatever is in data1 could be longer than the buffer though, and be sure to account for that scenario.
1

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); 
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.