` 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