-1
i yapı ve işaretçileri kullanarak basit dostuma listesini yaptı ama onun doğru olanı baskı yapmadığı 'kod biraz problem varyapı ile algoritma ve işaretçileri
typedef struct friends {
int namesize;
char* myfriends;
}friends;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(void)
{
int numfriends = 0;
int*p_friends = &numfriends;
int i = 0, count = 0;
printf("Welcome , here is a list of your friends \n");
printf("plesase enter the number of your friends \n");
scanf("%d", &numfriends); //gets the number of the friends
printf("you have %d friends \n", numfriends);
friends *f= malloc(sizeof(friends) * numfriends); // malloc
printf("now please enter thier'S name \n");
for (i = 0; i < numfriends; i++)
{
count++; // for looking at the number of the friend
(f+i)->myfriends= (char*)malloc(sizeof(char) * numfriends); // malloc
printf("enter the name of friend NO.%d ", count);
fgets((f + i)->myfriends,20,stdin); // entering the names of the friends
(f +i)-> namesize = strlen((f + i)->myfriends) + 1;
((f + i)->myfriends) = (char*)realloc((f + i)->myfriends, sizeof(char) * numfriends * (f + i)->namesize); //realloc
}
for (int j = 0; j < numfriends; j++)
{
printf("The name of friend NO %d is: %s \n", j + 1, ((f + j)->myfriends)); //printing the names
free((f + j)->myfriends);
}
system("pause");
return 0;
}
ve im Arkadaş listesini yazdırmaya çalışırken :
Bunu nasıl çözebilirim?
-> MyFriends = (char *) malloc (sizeof (char) * numfriends);' fgets ((f + i) 'tarafından takip -> MyFriends , 20, stdin); İpucu: Yaptığını düşündüğünüz şeyi yapmıyor çünkü her ikisi de (f + i) -> arkadaşlarıma farklı bilgiler yazıyor. –
Sadece arkadaşça bir ipucu, bu sayfada okumak isteyebilirsiniz: [Nasıl Yapılır Kılavuzu] (https://stackoverflow.com/help/how-to-ask) böylece her zaman sorularınızın olduğundan emin olabilirsiniz. kolayca cevaplanabilir ve olabildiğince açık. Yaşadığınız sorunu çözmek için yaptığınız tüm çabaları ve bu düzeltmeleri denediğinizde ne olduğunu eklemeyi unutmayın. Ayrıca, gösteri kodunuzu ve herhangi bir hata mesajınızı da unutmayın! –