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?

user2081328
  • 159
  • 3
  • 15
  • 1
    Is this library statically or dynamically linked to your project? – Bathsheba Mar 03 '14 at 13:36
  • Have you included the necessary headers – tesseract Mar 03 '14 at 13:38
  • It's statically connected. But if there is difference it would be good to know both :D – user2081328 Mar 03 '14 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. – user2081328 Mar 03 '14 at 13:41
  • 1
    It would help if you could provide a minimal working example (see http://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?) – benj Mar 03 '14 at 13:43
  • I populate array from serial port using: `open(file_desc, buffer, sizeof(buffer));` and get array of data that I wont to read as hexadecimal values in main function. But that is not relevant, I just want to know how to use char array created in file1.c in file2.c. I can do this with variables but not with arrays – user2081328 Mar 03 '14 at 13:50

2 Answers2

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;
}
Flovdis
  • 2,945
  • 26
  • 49
  • 2
    `new` and `delete[]` are not plain C as the question is tagged. Would be convenient if you could use the C equivalents. – halex Mar 03 '14 at 13:57
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 */
Community
  • 1
  • 1
benj
  • 713
  • 6
  • 12
  • 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 – user2081328 Mar 03 '14 at 14:18