` struct mylist_in { int enrollment_no; 字符串名称< 10>; int age; 字符串地址< 100>; mylist_in * next; mylist_in * next; }; struct mylist_out { int res; }; 节目RECORD版本REC_VER mylist_out recdis(mylist_in)= 1; } = 1; } = 0x31240000;获取指向数组的另一个指针指向的循环中char数组的值
#include "student.h"
#include <string.h>
#include <malloc.h>
int main(int argc, char **argv)
{
CLIENT *c1;
char name,address;
c1 = clnt_create(argv[1],RECORD,REC_VER,"tcp");
int n,i,j,k;
printf("Enter the no. of records to store\n");
scanf("%d",&n);
mylist_in data[n+1];
mylist_out *result;
printf("Enter the details.\n");
for(i=0;i<n;i++)
{
printf("Enter space separated enrollment_no, name, age, address.\n");
scanf("%d %s %d %s",&data[i].enrollment_no,&name,&data[i].age,&address);
data[i].name = name;
data[i].address = address;
}
for(j=0;j<n-1;j++)
{
data[j].next = &data[j+1];
}
data[n-1].next = NULL;
if((result = recdis_1(&data[0],c1))==NULL)
{
printf("Error\n");
return(0);
}
printf("%d records written to the file\n",result->res);
exit(0);
}
#include "student.h"
#include <stdio.h>
mylist_out*
recdis_1_svc(mylist_in *inp, struct svc_req *rqstp)
{
printf("Start \n");
static mylist_out out;
int k=0;
FILE *f = fopen("record.txt", "w");
if (f == NULL)
{
out.res = -1;
exit(1);
}
out.res = 0;
;
while (inp != NULL)
{
fprintf(f, "RECORD # %d\nenr_no = %d, name = %s, age = %d, address = %s \n",k+1,inp>enrollment_no,inp->name,inp->age,inp->address);
k++;
inp = inp->next;
}
out.res = k;
fclose(f);
return(&out);
}
`我有一个字符数组名称其中i使用在c scanf()的函数的一些值存储在一个for循环。有一个struct其中包含一些元素以及字符指针data.name。现在,当我尝试存储值名称在data.name结构的不同元素的字段和执行循环后,当我尝试打印的值为data.name一个接一个,所有它们给出相同的值,即名称中最后一个值。我猜它是因为所有的数据元素都指向名称变量,并且在scanf()操作期间覆盖这些值。有人可以帮我解决这个问题吗?
请问您可以分享问题代码吗? – Mureinik 2014-09-27 10:27:13
..因为这将是得心应手的 – 2014-09-27 10:27:28
任何代码来表示你的长文本? – 2014-09-27 10:31:13