首先,请参阅此代码。
#include <stdio.h>
#include <string.h>
#define N 3
struct body
{
char p[3];
char v[3];
char a[3];
double radius;
double mass;
};
struct body bodies[N];
int main() {
int a, b;
for(a = 0; a < 1; a++) {
for(b =0; b < 1; b++) {
strncpy(&bodies[a].p[b] , "asd", sizeof(3));
strncpy(&bodies[a].v[b] , "bbb", sizeof("bbb"));
strncpy(&bodies[a].a[b] , "ccc", sizeof("ccc"));
printf("\n1. = %s\n",&bodies[a].p[b]);
printf("\n2. = %s\n",&bodies[a].v[b]);
printf("\n3. = %s\n",&bodies[a].a[b]);
}
bodies[a].mass = 0;
bodies[a].radius= 1.0;
}
return 0;
}
,结果是
= asdbbbccc
= bbbccc
= ccc
我想要一行中的一个字符! 例如
为什么这样打印? T.T 是那个指针问题?????
帮帮我〜
答案 0 :(得分:0)
你的问题在这里:
struct body
{
char p[3];
char v[3];
char a[3];
double radius;
double mass;
};
C字符串是null terminated,这意味着他们需要一个额外的空字符'\0'
来表示字符串的结尾,所以如果你想存储一个3字符的字符串,你需要分配4容纳额外空终止符的字符。试试这个:
struct body
{
char p[4];
char v[4];
char a[4];
double radius;
double mass;
};
答案 1 :(得分:0)
,结果是
- = asdbbbccc
- = bbbccc
醇>
<强>原因:强>
这是由于您的代码中的某些因素导致的结果......
char a[3]
可以在a[0]
和a[1]
处包含两个字符以及'\0'
处的一个终止a[size-1]
字符,即{{1} }} a[3-1]
字符'\0'
,char p[3];
,char v[3];
按顺序排列,char a[3];
时,会打印从printf("\n1. = %s\n",&bodies[a].p[0]);
到bodies[a].p[0]
字符,这在您的任何字符串中都不存在。因此,'\0'
继续打印字符串printf()
,bodies[a].v
,直到遇到未定义的bodies[a].a
。'\0'
时会发生这种情况......它会继续打印printf("\n1. = %s\n",&bodies[a].v[0]);
,直到遇到未定义的bodies[a].a
:( <强>解决方案:强>
为了避免这种提供 '\0'
字符的空间,请不要过度编写...这样:
'\0'
代码中的又一个问题是struct body
{
char p[4]; //space for 3 letters along with one '\0' character
char v[4];
char a[4];
double radius;
double mass;
};
因为,sizeof(3)=4
是一个整数,在您的系统中int占用3
个字节。但是,您的代码中字符串的大小为4
。要解决此问题,只需将3
代替3
作为参数
将代码放在一起将是:
sizeof(3)
<强>输出强>:
#include <stdio.h>
#include <string.h>
#define N 3
struct body
{
char p[4]; //size = 4
char v[4];
char a[4];
double radius;
double mass;
};
struct body bodies[N];
int main() {
int a,b;
for(a = 0; a < 1; a++) {
for(b =0; b < 1; b++) {
strncpy(&bodies[a].p[b] , "asd", 3); //size = 3 bytes
strncpy(&bodies[a].v[b] , "bbb", 3); //size = 3 bytes
strncpy(&bodies[a].a[b] , "ccc", 3); //size = 3 bytes
printf("\n1. = %s\n",&bodies[a].p[0]);
printf("\n2. = %s\n",&bodies[a].v[0]);
printf("\n3. = %s\n",&bodies[a].a[0]);
}
bodies[a].mass = 0;
bodies[a].radius= 1.0;
}
return 0;
}
答案 2 :(得分:-4)
您可以将malloc
与C字符串(char *
)一起使用,以获得所需的结果:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 3
struct body
{
char * p;
char v[3];
char a[3];
double radius;
double mass;
};
struct body bodies[N];
int main() {
int a, b;
for(a = 0; a < 1; a++) {
for(b =0; b < 1; b++) {
bodies[a].p = malloc(3);
strncpy(bodies[a].p , "asd", sizeof(3));
printf("\n1. = %s\n",bodies[a].p);
}
bodies[a].mass = 0;
bodies[a].radius= 1.0;
}
return 0;
}
整个计划将成为:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 3
struct body
{
char * p;
char * v;
char * a;
double radius;
double mass;
};
struct body bodies[N];
int main() {
int a, b;
for(a = 0; a < 1; a++) {
for(b =0; b < 1; b++) {
bodies[a].p = malloc(3);
bodies[a].v = malloc(3);
bodies[a].a = malloc(3);
strncpy(bodies[a].p , "asd", sizeof(3));
strncpy(bodies[a].v , "bbb", sizeof("bbb"));
strncpy(bodies[a].a , "ccc", sizeof("ccc"));
printf("\n1. = %s\n",bodies[a].p);
printf("\n2. = %s\n",bodies[a].v);
printf("\n3. = %s\n",bodies[a].a);
}
bodies[a].mass = 0;
bodies[a].radius= 1.0;
}
return 0;
}
测试输出
1. = asd
2. = bbb
3. = ccc
Process finished with exit code 0