2

I wish to split a "string" by the character ','. The string holds a GPS NMEA encoded string, but that is of no matter.

My problem is that sometimes the parameter from the function that processes this char array is empty... Like nothing is in the array.

How should I correctly pass a "char string[]" to a function so that I may operate on a that parameter as I sent it(as a char array, not a char pointer to an array).

I also need to specify that I'm using mikroC for PIC.

Here is my code as of right now:

char* GPS_sateliti;
char CsatInView[] = 
"$GPGSV,3,2,11,14,25,170,00,16,57,208,39,18,67,296,40,19,40,246,00*74";

GPS_sateliti = GrupeazaDupaVirgule(CsatInView, 2);

char* GrupeazaDupaVirgule( char deGasit[],int nrVirgule ){
    int cVirgule = 1;
    char* pch = strtok (deGasit,",");

    while (pch != 0)
    {
       pch = strtok (0, ",");
       cVirgule++;
       if(nrVirgule == cVirgule){
          break;
    }
}
return pch;
}

The function that operates on the char array received as a parameter in debug mode, before entering the function the char array is fine, after entering it, it seems to be empty The function that operates on the char array received as a parameter in debug mode, before entering the function the char array is fine, after entering it, it seems to be empty

It may be that I should receive a pointer to an array of chars?? Any sort of advice is welcome. Thank you

alessandrio
  • 4,282
  • 2
  • 29
  • 40
Fluffy
  • 23
  • 1
  • 7
  • How did you determine that it "was fine" before the function? – Scott Hunter Jun 01 '17 at 12:47
  • How do you *call* the function? That's where the problem must be. Please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us. Also, while you're debugging, why didn't you step through the code before the call to see what happens with the string being passed to the function? – Some programmer dude Jun 01 '17 at 12:47
  • Oh and by the way, the `strtok` function *modifies* the string it tokenizes (see [this `strtok` reference](http://en.cppreference.com/w/c/string/byte/strtok) for more information). Could this be a problem? – Some programmer dude Jun 01 '17 at 12:48

2 Answers2

2

How should I correctly pass a "char string[]" to a function so that I may operate on a that parameter as I sent it(as a char array, not a char pointer to an array).

You can't. A function parameter of an array type always decays as the corresponding pointer type.

There are two idiomatic solutions to this.

1. a sentinel:

The last value in the array is a special value that marks the end. This is done in C with strings. They always end with a \0 character, that is guaranteed not to occur inside the string. The function can search for that character to know where the data ends.

(Note: with this info I have to add I'm not sure what your problem is. If you pass an "empty string", as literally "", the \0 will be there, so you shouldn't have a problem)

2. explicitly passing the size:

instead of just

void foo(int bar[]);

you define a function

void foo(size_t barSize, int bar[]);

The caller knows the size of the array, so it can just pass it along.

Community
  • 1
  • 1
  • Ok, I will try it this way, thanks for clearing things up :) Would you mind detailing up a bit how I can remake my array up in the function? I guess by iterating throught the char* with a for and stop at the size index?? – Fluffy Jun 01 '17 at 13:05
0

With a pointer :

char* arr; 
yourFunction(arr);

If you wish to initialize it before :

char* arr = malloc(51 * sizeof(char)); // Allocate a memory place of 50 because strings are null terminated in C
yourFunction(arr);

An other way to allocate memory to an array :

char* arr = calloc(50, sizeof(char)); // Allocate 50 memory place which size if the size of a char

With a string :

char arr[50];
char* ptr = arr;
yourFunction(ptr);

You have to know that it is impossible in C to know the size of an array when using pointer. The only thing you can do is to parse the size of the string as a parameter :

size_t size = 50;
char arr[size];
char* ptr = arr;
yourFunction(ptr, size);

If you wish to understand in detail how pointer works and how to iterate them, may be this post can help you. I think it is very interesting. Globally, you iterate through an array via a pointer like this :

for ( int i = 0; i < size; i++)
    printf("Current pointed value in the array : %c\n", ptr[i]); // or arr[i]

I guess you understand why giving the size of a pointed array as a parameter is important. Sometimes you can avoid using this parameter like this :

for ( int i = 0; i != '\0'; i++) // Because strings are null-terminated in C.
    // Do something
Badda
  • 1,329
  • 2
  • 15
  • 40
  • Thank you, when I first read Felix's answer I must admit I was a bit confused but you cleared out how to determine size of my array. – Fluffy Jun 01 '17 at 13:02
  • If you need, [malloc](http://www.cplusplus.com/reference/cstdlib/malloc/) and [calloc](http://www.cplusplus.com/reference/cstdlib/calloc/) documentations. Don't worry if it memory allocation seems complicated : it is one of the main error reason in C and one of the most tricky feature of this language, if not the most. – Badda Jun 01 '17 at 13:06
  • @Fluffy I edited my answer to explain a bit more how to use a pointer to a string in a function. Hope it will help – Badda Jun 01 '17 at 13:14
  • Oh my God, you made it way too easy for me :) now I know exactly how to do with my array, I will build a new one inside the function since I might need it for another thing, many thanks :o – Fluffy Jun 01 '17 at 13:19