简体   繁体   English

C编程指针和char数组问题

[英]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. 你必须在这里变量范围的问题: theMessage是本地功能search_value ,所以你返回一个指针不再存在,一旦函数完成的数组。

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. 相反,你应该使用malloc()theMessage分配空间,然后在你完成它之后随后在函数外面free()free()但是如果你不勤于清理,这通常会导致内存泄漏追求自己。

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: 另一种方法是在getMessage()分配缓冲区,并将指向缓冲区的指针传递给search_value ,然后可以将其写入:

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. 另一种选择是在getMessage()声明一个char *指针,将一个指针传递给一个指向search_value()的指针,然后再次使用malloc()为缓冲区分配空间。

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. 最后,这是一个次要的风格批评,但你要学会坚持一个命名你的函数的惯例, search_valuegetMessage不是一致的名字,这将使许多你工作的编码器感到search_value

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. calle函数应该提供它期望值的数组或使用静态数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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