0

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?

6
  • 1
    Is this library statically or dynamically linked to your project? Commented Mar 3, 2014 at 13:36
  • Have you included the necessary headers Commented Mar 3, 2014 at 13:38
  • It's statically connected. But if there is difference it would be good to know both :D Commented Mar 3, 2014 at 13:38
  • Yes I have. In this particular case that I have shown It displays error. If I use printf("%i", myArray[i]) it works but shows wrong data. Commented Mar 3, 2014 at 13:41
  • 1
    It would help if you could provide a minimal working example (see stackoverflow.com/help/mcve for guidelines). The code you posted isn't complete, so it's hard to see what you're doing (for instance: how do you populate the array? What's the form of the for loop in the main() function?) Commented Mar 3, 2014 at 13:43

2 Answers 2

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.

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

new and delete[] are not plain C as the question is tagged. Would be convenient if you could use the C equivalents.
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).

  • 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 */

1 Comment

This works, but doesn't solve my problem. I have buffer with some characters buffer[100] = { 0x2, 0x17, 0x20 etc.. } and I wish to access each of these characters individually. buffer is populated with data in function in libfile.c and I want to read these data in function in program.c

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.