首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

关于字符串指针和数组的有关问题

2012-02-22 
关于字符串指针和数组的问题这个如何才能编译过? 请大家帮忙看看include stdio.hinclude stdlib.hint

关于字符串指针和数组的问题
这个如何才能编译过? 请大家帮忙看看

include <stdio.h>
include <stdlib.h>

int main()
{
  typedef struct _S_VALUE{
  char var[8+1];
  char val[100+1];
  }S_VALUE;
  char sz[50]={0};

  strcpy(sz,"abc");
  S_VALUE ar[2] = {
  "01",sz,
  "",""}; 
  return 0;
}


[解决办法]

C/C++ code
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){    typedef struct _S_VALUE{        char var[8+1];        char val[100+1];    }S_VALUE;    char sz[50]={0};        strcpy(sz,"abc");    S_VALUE ar[2] = {"01","abc","",""};          return 0;}
[解决办法]
类型不同,这样也行:
S_VALUE ar[2];

strncpy(ar[0].var,sz,sizeof(ar[0].var));
strncpy(ar[0].val,sz,sizeof(ar[0].val));

//........

[解决办法]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
typedef struct _S_VALUE{
char var[8+1];
char val[100+1];
}S_VALUE;
char sz[50]={0};

strcpy(sz,"abc");
S_VALUE ar[2] = {{"01","abc"},{"",""}};
return 0;
}

你也可以尝试使用其他方法对结构体变量进行赋值,比如使用ar[0].var, ar[0].val等等吧。
[解决办法]
C/C++ code
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){  typedef struct _S_VALUE{  char var[8+1];  char val[100+1];  }S_VALUE;  char sz[50]={0};      strcpy(sz,"abc");  S_VALUE ar[2] = {{"01",sz},{"",""}};     return 0;}
[解决办法]
int main()
{
typedef struct _S_VALUE{
char var[8+1];
char val[100+1];
}S_VALUE;

//char sz[50]={0};

//strcpy(sz,"abc");
S_VALUE ar[2] = {
{"01","abc"},
{"",""}
};
}
[解决办法]
C/C++ code
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){  typedef struct _S_VALUE{  char var[8+1];  char *val;  }S_VALUE;  char sz[50]={"abc"};      //strcpy(sz,"abc");  S_VALUE ar[2] = {{"01",sz}, {"0","1"}};      //for test  printf("%s %s\n", ar[0].val, ar[0].var);  printf("%s %s\n", ar[1].val, ar[1].var);  return 0;} 

热点排行