Jump to content

dynamic struct arrays problem


Recommended Posts

Posted (edited)

Boas.

Estou a fazer um programa com o objetivo de usar as structs e um vetor das structs mas queria alocalo dinamicamente ou seja comessar com um vetor de uma posição e ir aumentando conforme necessário e apreciava ter uma ajuda.

#include <stdio.h>
#include <stdlib.h>
#define MAX 100
#define Vi 1

struct Aluno
{
   char* Nome[MAX];
   char* Morada[MAX];
   char* Curso[MAX];
   int*  Idade;  
} aluno[Vi];

int main(int argc, char** argv) {
    
    
    int deci,contv=0,conta=1;
    
      printf("Introduza um nº:  \n");
      scanf("%d",&deci);
    
    while(deci != 0)
    {
        
      printf("Insira o nome do Aluno %d : \n",conta);
      aluno[Vi].Nome=(char*) malloc((Vi+contv)*sizeof(char));
      scanf("%s",&aluno[contv].Nome);
      
      printf("Insira a morada do Aluno %d : \n",conta);
      aluno[Vi].Morada=(char*) malloc((Vi+contv)*sizeof(char));
      scanf("%s",&aluno[contv].Morada);
      
      fflush(stdin);
      
      printf("Insira o seu Curso %d : \n",conta);
      aluno[Vi].Curso=(char*) malloc((Vi+contv)*sizeof(char));
      scanf("%s",&aluno[contv].Curso);
      
      fflush(stdin);
      
        printf("Insira a Idade do aluno %d: \n",conta);
        aluno[Vi].Idade=(int*) malloc((Vi+contv)*sizeof(int));
        scanf("%d",&aluno[contv].Idade);
      
      printf("Introduza um nº:  \n");
      scanf("%d",&deci);
      
      conta++;
      contv++;
        
    }
    
    

    return (EXIT_SUCCESS);
}

 

gostava de sugestões e obrigado 🙂
 

Edited by darkxD

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.