如何在c中从库中传递数组

时间:2021-10-30 21:20:16

I'm new in c programming and I want to pass array from library. I have function in library c file that creates char array. How to use this array in main function. This is short code of something I tried:

我是c编程的新手,我想从库中传递数组。我在库c文件中有函数创建char数组。如何在main函数中使用此数组。这是我尝试过的简短代码:

libfile.c

libfile.c

char *myArray;
void PopulateArray()
{
  // Getting data from serial port in char buffer[100]
  myArray = buffer;
} 

libfile.h

libfile.h

exter char *myArray;
void PopulateArray();

program.c

program.c

int main()
{
   // in fore loop
  printf("%s\n" , myArray[i]);
}

This is just one of combinations that I have tried but nothing works. How to do this?

这只是我尝试过的一种组合,但没有任何效果。这个怎么做?

2 个解决方案

#1


1  

To pass an array from a library function to the surrounding code, you can use the return value of a function or use a pointer-to-pointer argument.

要将数组从库函数传递给周围的代码,可以使用函数的返回值或使用指针指针参数。

See the following example code.

请参阅以下示例代码。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* createSourceCopy() {
    const char *source = "Example Text";
    // We got some text in variable source;
    const size_t sourceSize = strlen(source);
    char *result = (char*)malloc(sizeof(char)*(sourceSize+1));
    strncpy(result, source, sourceSize);
    return result;
}

A user of your library could use the function like this:

您的库的用户可以使用如下函数:

main() {
    char *result = createSourceCopy();
    // Do something with result.
    // After the use, destroy the array
    delete[] result;
    return 0;
}

Another way how to pass an array is this:

另一种传递数组的方法是:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

bool copySourceText( char **outText ) {
    const char *source = "Example Text";
    // We get some text in variable source;
    const size_t sourceSize = strlen(source);
    *outText = new char[sourceSize];
    strncpy(*outText, source, sourceSize);
    return true; // success
}

This second variant has the benefit that the return value can be used as status. The function could return true on success, or false if there was an error.

第二种变体的好处是返回值可以用作状态。该函数可以在成功时返回true,如果有错误则返回false。

This second version can be used like this.

第二个版本可以像这样使用。

int main() {
    char *result;
    if (copySourceText(&result)) {
        // Do something with result.
        // After the use, destroy the array
        free(result);
        result = NULL;
    } else {
        // Error handling
    }
    return 0;
}

#2


0  

It's not clear exactly what's going wrong in the code you posted (it would help to see more code), but assuming your problem isn't a compilation error, one of these lines might be wrong:

目前尚不清楚您发布的代码到底出了什么问题(这有助于查看更多代码),但假设您的问题不是编译错误,其中一行可能是错误的:

char *myArray;
printf("%s\n" , myArray[i]);
  • char *myArray declares a pointer to char (which would be appropriate for a single string).

    char * myArray声明一个指向char的指针(适用于单个字符串)。

  • The printf line dereferences myArray (producing a char, i.e. one character). You're passing down a char, but the %s format expects a pointer-to-char.

    printf行取消引用myArray(生成一个char,即一个字符)。你传递了一个char,但%s格式需要一个指向char的指针。

If you want to print the string character-by-character, you could use %c:

如果要逐个字符地打印字符串,可以使用%c:

for (i = 0; i < length; i++) {
    printf("%c\n", myArray[i]);   /* or %x or %d if you want */
}

Otherwise, if myArray is one string and is null-terminated (see Why is a null terminator necessary?), then you could do:

否则,如果myArray是一个字符串并且以null结尾(请参阅为什么需要一个空终止符?),那么你可以这样做:

printf("%s\n" , myArray);   /* [i] removed, no for loop necessary */

#1


1  

To pass an array from a library function to the surrounding code, you can use the return value of a function or use a pointer-to-pointer argument.

要将数组从库函数传递给周围的代码,可以使用函数的返回值或使用指针指针参数。

See the following example code.

请参阅以下示例代码。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* createSourceCopy() {
    const char *source = "Example Text";
    // We got some text in variable source;
    const size_t sourceSize = strlen(source);
    char *result = (char*)malloc(sizeof(char)*(sourceSize+1));
    strncpy(result, source, sourceSize);
    return result;
}

A user of your library could use the function like this:

您的库的用户可以使用如下函数:

main() {
    char *result = createSourceCopy();
    // Do something with result.
    // After the use, destroy the array
    delete[] result;
    return 0;
}

Another way how to pass an array is this:

另一种传递数组的方法是:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

bool copySourceText( char **outText ) {
    const char *source = "Example Text";
    // We get some text in variable source;
    const size_t sourceSize = strlen(source);
    *outText = new char[sourceSize];
    strncpy(*outText, source, sourceSize);
    return true; // success
}

This second variant has the benefit that the return value can be used as status. The function could return true on success, or false if there was an error.

第二种变体的好处是返回值可以用作状态。该函数可以在成功时返回true,如果有错误则返回false。

This second version can be used like this.

第二个版本可以像这样使用。

int main() {
    char *result;
    if (copySourceText(&result)) {
        // Do something with result.
        // After the use, destroy the array
        free(result);
        result = NULL;
    } else {
        // Error handling
    }
    return 0;
}

#2


0  

It's not clear exactly what's going wrong in the code you posted (it would help to see more code), but assuming your problem isn't a compilation error, one of these lines might be wrong:

目前尚不清楚您发布的代码到底出了什么问题(这有助于查看更多代码),但假设您的问题不是编译错误,其中一行可能是错误的:

char *myArray;
printf("%s\n" , myArray[i]);
  • char *myArray declares a pointer to char (which would be appropriate for a single string).

    char * myArray声明一个指向char的指针(适用于单个字符串)。

  • The printf line dereferences myArray (producing a char, i.e. one character). You're passing down a char, but the %s format expects a pointer-to-char.

    printf行取消引用myArray(生成一个char,即一个字符)。你传递了一个char,但%s格式需要一个指向char的指针。

If you want to print the string character-by-character, you could use %c:

如果要逐个字符地打印字符串,可以使用%c:

for (i = 0; i < length; i++) {
    printf("%c\n", myArray[i]);   /* or %x or %d if you want */
}

Otherwise, if myArray is one string and is null-terminated (see Why is a null terminator necessary?), then you could do:

否则,如果myArray是一个字符串并且以null结尾(请参阅为什么需要一个空终止符?),那么你可以这样做:

printf("%s\n" , myArray);   /* [i] removed, no for loop necessary */