传递具有空第一索引的非空字符串数组
Passing non empty array of strings with null first indexes
我需要将一个字符串数组传递给函数,我只想填充数组的第三项,并保留所有其他项NULL
,以便我可以将其填充到我传递的函数中数组到。但不幸的是,它将传递的数组读取为所有项目的 NULL
。我不明白这是为什么?
它读取 Msg
数组就好像它是全部 NULL
,而不读取已经设置的第 7 项。
这是我的代码:
void MSGCONT()
{
char *Front= "0811";
char *DateTime = "1701231335";
char *Msg[130];
int i = 0;
while (i < 130) {
Msg[i] = '[=10=]';
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
}
}
首先,您需要决定是否要为未填充的数组项传递NULL 或空字符串。我建议 NULL 是为了减少歧义,但出于实用的原因,您可能会选择空字符串。
如果您选择使用 NULL,则必须先检查数组中的指针,然后再尝试访问它们所指向的内容。
无论哪种方式,您都应该将数组中元素的数量作为参数传递给 Build。
让我们看一个带有 NULL 的版本:
#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;
}
这是一个空字符串版本:
#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;
}
我需要将一个字符串数组传递给函数,我只想填充数组的第三项,并保留所有其他项NULL
,以便我可以将其填充到我传递的函数中数组到。但不幸的是,它将传递的数组读取为所有项目的 NULL
。我不明白这是为什么?
它读取 Msg
数组就好像它是全部 NULL
,而不读取已经设置的第 7 项。
这是我的代码:
void MSGCONT()
{
char *Front= "0811";
char *DateTime = "1701231335";
char *Msg[130];
int i = 0;
while (i < 130) {
Msg[i] = '[=10=]';
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
}
}
首先,您需要决定是否要为未填充的数组项传递NULL 或空字符串。我建议 NULL 是为了减少歧义,但出于实用的原因,您可能会选择空字符串。 如果您选择使用 NULL,则必须先检查数组中的指针,然后再尝试访问它们所指向的内容。
无论哪种方式,您都应该将数组中元素的数量作为参数传递给 Build。
让我们看一个带有 NULL 的版本:
#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;
}
这是一个空字符串版本:
#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;
}