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;
}
Msg[i] = '\0';-->Msg[i] = "\0";,Msgis an array of strings, not a string 2) Compare strings usingstrcmp()instead of==,while(Msg[1] == "")-->while (strcmp(Msg[1], "") == 0)Msg[i] = "\0";->Msg[i] = "";. The extra null byte is useless.