简体   繁体   English

如何在结构中直接将指针分配给新结构?

[英]How to assign a pointer in struct directly to a new struct?

can you help me please? 你能帮我吗?

I want to manipulate a doubly linked liste that manage a bank accounts, but I don't know why it doesn't work even if I tried vainly to correct all my mistakes. 我想操纵一个管理银行帐户的双向链接列表,但是即使我徒劳地纠正所有错误,我也不知道为什么它不起作用。 Certainly, It's the first time that i use this nested structer and i don't know how to access from the main structre to the other ones (idt,idf). 当然,这是我第一次使用此嵌套的构造函数,而且我不知道如何从主结构访问其他结构(idt,idf)。

2 Questions: 2个问题:

-is this method accepted "pnt->idt.dn.j_n" (pnt is a pointer on CompteBancaire)? -此方法是否接受“ pnt-> idt.dn.j_n”(pnt是CompteBancaire上的指针)?

-when i traverse the main linked liste using a pointer and i want add a particular struct to my new liste (YoungCustomers), is there any possibility to assign the struct pointer to a new struct that i'll put it in my new liste, instead of assigning each element to it? -当我使用指针遍历主链接列表并且想要将一个特定的结构添加到我的新列表(YoungCustomers)中时,是否有可能将结构指针分配给一个新结构,我将其放置在新列表中,而不是分配每个元素呢? Thanks in advance. 提前致谢。

#include <stdio.h>
#include <stdlib.h>
#include <string.h >
typedef struct
{  int j_n,m_n,a_n;
}dateDeNaissance;

typedef struct
{  char nom[15];
   char prenom[15];
    dateDeNaissance dn;
}identite;

typedef struct
{  int numCompte;
   char nomBanque[20];
}identifiant;
/* ------------THE MAIN STRUCT-------------*/
typedef struct
{  identite idt;
   identifiant idf;
   float solde;
   struct CompteBancaire *next;
   struct CompteBancaire *prc;
}CompteBancaire;
/* -----------STRUCT OF YOUNG CUSTOMERS----------------*/
typedef struct
{ CompteBancaire *Cpt;
 struct ClientJeune *next;
}ClientJeune;
/*-------------GLOBAL POINTERS------------- */
CompteBancaire *first=NULL;
CompteBancaire *first2=NULL;
/*---------- CREATE A BANC ACOUNTE ---------*/
void saisiNovCompt (char nom[],char prn[],int j,int m,int a,int numCpt,char nomB[],float Solde) //21:32
{  CompteBancaire *nov=malloc(sizeof(CompteBancaire)),pnt=first;
   strcpy (nov->idt.nom,nom);
   strcpy (nov->idt.prenom,prn);
   nov->idt.dn.j_n=j;
   nov->idt.dn.m_n=m;
   nov->idt.dn.a_n=a;
   nov->idf.numCompte=numCpt;
   nov->prc=NULL;
   strcpy (nov->idf.nomBanque,nomB);
   nov->solde=Solde;
   if (first==NULL)
   {  nov->next=NULL;
      nov->prc=NULL;
      first=nov;
   }
   else
   {  first=nov;
      nov->next=pnt;
      pnt->prc=nov;
   }
}
/* --------SAVE AN ACCOUNTE IN FILE-------------*/
void fichier (CompteBancaire *pnt)
{  FILE *Compte;
   Compte=fopen("comptBancaire.txt","w");
   fprintf("nom: %s\nprenom: %s\ndate de naissance: %d/%d/%d\nnum de compte: %d\nnom de la banque: %s\nsolde: %.2f\n",
           pnt->idt.nom,pnt->idt.prenom,pnt->idt.dn.j_n,pnt->idt.dn.m_n,pnt->idt.dn.a_n,pnt->idf.nomCompte,pnt->idf.nomBanque);
  fclose(Compte);
}
/* -------------- SAVE AN ACCOUNT THAT IT'S NUMBER IS GIVEN BY THE USER ------------------*/
void afficheParNum (int numCpt)
{  CompteBancaire *pnt=first;
   if (first==NULL)
      printf ("la liste est vide \n");
   else
   {  while (pnt!=NULL && pnt->idf.numDeCompt!=numCpt)
             pnt=pnt->next;
      if (pnt==NULL)
         printf("Compte non trouve !\n");
      else
         fichier(pnt);
   }
}
/*-----------------REMOVE AN ACCOUNT THAT IT'S NUMBER IS GIVEN BY THE USER--------------------*/
void suppCompt (int numCpt)
{  CompteBancaire *pnt=first;
   if (first==NULL)
       printf("la liste vide !\n");
   else
   {   while (pnt!=NULL && pnt->idf.numCompte!=numCpt)
              pnt=pnt->next;
       if (pnt==first) // remove in beginning
       {  first=pnt->next;
          free(pnt);
       }
       else if (pnt->next==NULL) // remove at the end
       {  (pnt->prc)->next=NULL;
           free(pnt);
       }
       else  // remove in the middle
       {  (pnt->prc)->next=pnt->next;
          (pnt->next)->prc=pnt->prc;
           free(pnt);
       }
       }
   }
   }
/* ------------------REMOVE THE ACCOUNT IF THE BALANCE IS LESS THEN 0 DH--------------------------*/
void suppInf00 ()
{CompteBancaire *pnt2=first,*pnt=NULL;
   if (first==NULL)
       printf("la liste vide !\n");
   else
   {   while (pnt2!=NULL) {
       pnt=pnt2;
       if (pnt->solde <0) {
       if (pnt==first) // remove in the beginning
       {  first=pnt->next;
          free(pnt);
       else if (pnt->next==NULL) // remove at the end
       {  (pnt->prc)->next=NULL;
           free(pnt);
       }
       else  // remove in the middle
       {  (pnt->prc)->next=pnt->next;
          (pnt->next)->prc=pnt->prc;
           free(pnt);
       }
       }
        pnt2=pnt2->next;
   }
   }
   }

}
/* -----------CREAT A LISTE OF YOUNG PEAPLE--------------------- */
void under35 ()
{  ClientJeune nov,*pnt=first;
   if (first==NUll)
      printf("liste vide !\n");
   else
   {  while (pnt!=NULL)
      if (2015-(pnt->idt.dn.a_n)<=35)
      {   nov->Cpt=pnt;
          if (first2==NULL)
             nov->next=NULL;
           else
           {  nov->next=first2;
              first2=nov;
           }
      }
   }
}
/* -----------------DISPLAY ALL ACCOUNTS---------------------*/
void afficage ()
{CompteBancaire *pnt=first;
 if(first==NULL)
    printf("liste vide !\n");
 else
 { while (pnt!=NULL)
   printf("nom: %s\nprenom: %s\ndate de naissance: %d/%d/%d\n num de compte: %d\nnom de la banque: %s\nsolde: %.2f\n\n",
           pnt->idt.nom,pnt->idt.prenom,pnt->idt.dn.j_n,pnt->idt.dn.m_n,pnt->idt.dn.a_n,pnt->idf.nomCompte,pnt->idf.nomBanque);
     pnt=pnt->next;
 }

}
int main()
{int n=0,c,j,m,a,numC;
 float S=0;
char A[15],B[15],C[15];
printf("-----------------Menu-------------------\n");
printf("1----------------------------------Saisi\n");
printf("2----------------------------------affichage par num de compte \n");
printf("3----------------------------------Suppression d'un compte \n");
printf("4----------------------------------Supp inf 00 DH\n");
printf("5----------------------------------under 35ans\n");
printf("6----------------------------------Affichage\n\n");
while (n==1)
{
    printf("donner votre choix :");
    scanf("%c",&c);
    switch (c)
    { case 1:printf("donner le nom:");scanf("%s",A);
             printf("donner le prenom:");scanf("%s",B);
             printf("donner la date de naissance jj mm aa:");scanf("%d %d %d",&j,&m,&a);
             printf("donner le num du compte:");scanf("%d",&numC);
             printf("donner le nom de la banque:");scanf("%s",C);
             printf("donner le solde :");scanf("%f",&S);
             saisiNovCompt(A,B,j,m,a,numC,C,S);break;
     case 2:printf("donner le num du compte:");scanf("%d",&numC);
            afficheParNum(numC);break;
     case 3:printf("donner le num du compte:");scanf("%d",&numC);
            suppCompt(numC);break;
     case 4:suppInf00();
            printf("suppression solde inf 0 à ete efectue\n");break;
     case 5:under35 ();
            printf("une nouvelle liste a ete creer\n");break;
     case 6:affichage();
     default:printf("tapper 1/2/3/4/5/6 !!\n");
    }
    printf("tapper 1:");
    scanf("%d",&n);
}
    return 0;
}

I don't know if I understood correctly, but if you want to initialize a struct AND assigning it's adress to a pointer 我不知道我是否正确理解,但是如果您想初始化一个结构并将其地址分配给指针

Suppose you have 假设你有

struct XXX
{
    T1 arg1;
    T2 arg2;
    T3 arg3;
    .
    .
    .
};

you may want to do something like : 您可能想做类似的事情:

struct XXX *ptr = &(struct XXX){arg1, arg2, arg3, ...};

maybe this is not the most elegant way, but it works. 也许这不是最优雅的方法,但它确实有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM