user2081328
user2081328

Reputation: 169

How to pass array from library in c

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:

libfile.c

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

libfile.h

exter char *myArray;
void PopulateArray();

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?

Upvotes: 0

Views: 660

Answers (2)

benj
benj

Reputation: 723

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

  • 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.

If you want to print the string character-by-character, you could use %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:

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

Upvotes: 0

Flovdis
Flovdis

Reputation: 3095

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.

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

Upvotes: 1

Related Questions