0

I need to pass an array of strings to function, I just want to fill the third item of the array, and keep all other items NULL, so I can fill it in the function that I passed the array to. But unfortunately it reads the passed array as NULL for all its items. and I can't figure out why that?

It reads Msg array as if it is all NULL, and doesn't read item 7 which it has already been set.

Here is my code:

void MSGCONT()
{
    char *Front= "0811";
    char *DateTime = "1701231335";
    char *Msg[130];
    int i = 0;

    while (i < 130) {
        Msg[i] = '\0';
        i++;
    }

    Msg[7] = &DateTime[0];

    Build(Msg, Front);
}

char *Build(char *Msg[130], char *Front) {
    Msg[0] = Front;

    while (Msg[1] == "") { // access violation reading location error.
        // some code
    }
}
8
  • 1
    1) Msg[i] = '\0'; --> Msg[i] = "\0";, Msg is an array of strings, not a string 2) Compare strings using strcmp() instead of ==, while(Msg[1] == "") --> while (strcmp(Msg[1], "") == 0) Commented Feb 5, 2017 at 8:05
  • Either provide complete code or provide a proper MCVE Commented Feb 5, 2017 at 8:07
  • So many errors in such a small piece of code. Commented Feb 5, 2017 at 8:13
  • @KeineLust ok, but still it doesn't read Item 7, The "Msg" value when its been passed is '""' Commented Feb 5, 2017 at 8:21
  • 1
    @KeineLust: Msg[i] = "\0"; -> Msg[i] = "";. The extra null byte is useless. Commented Feb 5, 2017 at 10:08

1 Answer 1

1

First of all, you need to decide whether you want to pass NULL or empty strings for array items not filled. I propose NULL for being less ambiguous, but you might choose empty strings for pragmatic reasons. If you choose to use NULLs, you must check the pointers in your array before trying to access what they are pointing at.

Either way, you should pass the number of elements in the array as an argument to Build.

Let's look at a version with NULLs:

#include <stdio.h>
#define NUMBER_OF_MESSAGES 130

char* Build(char* Msg[], int MsgCnt, char *Front);

void MSGCONT()
{
    char* Front= "0811";
    char* DateTime =  "1701231335";
    char* Msg[NUMBER_OF_MESSAGES];
    int i =0;

    while( i < NUMBER_OF_MESSAGES)
    {
         Msg[i++] = NULL;
    }

    Msg[7] = DateTime;

    Build(Msg, NUMBER_OF_MESSAGES, Front);

}

char* Build(char* Msg[], int MsgCnt, char *Front)
{
   Msg[0] = Front;

   for (int i=1; i<MsgCnt; i++) 
   {
     if(Msg[i] != NULL)
     {
       printf("%ith item contains %s\n", i, Msg[i]);
     }
   }

   return "whatever";
}

int main(int argc, char*argv[]) {
   MSGCONT();
   return 0;
}

And here's at a version with empty strings:

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

#define NUMBER_OF_MESSAGES 130

char* Build(char* Msg[], int MsgCnt, char *Front);

void MSGCONT()
{
    char* Front= "0811";
    char* DateTime =  "1701231335";
    char* Msg[NUMBER_OF_MESSAGES];
    int i =0;

    while( i < NUMBER_OF_MESSAGES)
    {
         Msg[i++] = "";
    }

    Msg[7] = DateTime;

    Build(Msg, NUMBER_OF_MESSAGES, Front);

}

char* Build(char* Msg[], int MsgCnt, char *Front)
{
   Msg[0] = Front;

   for (int i=1; i<MsgCnt; i++) 
   {
     if(strcmp(Msg[i], "")!=0)
     {
       printf("%ith item contains %s\n", i, Msg[i]);
     }
   }

   return "whatever";
}

int main(int argc, char*argv[]) {
   MSGCONT();
   return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

well, still it hasn't solve my problem, still the same old issue, which is 'Msg' has been passed fine from 'MSGCONT()' with all its Items, but when I receive it in 'Build' function as an Empty array with no Items.
I've extended the examples a bit, so you can copy and compile them directly. They work as expected. (The second example had a bug in the original answer)

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.