Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Attribute return of a function to an array

this is my first question on StackOverflow ! 🙂
To be honest I’m about to destroy my whole setup.

My code is making me crazy.

My problem is, I am not able to fill a dynamic array with the return of a function.
My goal here is, for each array box, fill it with a random value of ‘randomizer’. I am not able to take the return of randomizer in the array box.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is the code:

main.c:

#include "functions.h"
#include "functions.c"

/*       TP 3 - ESIEE-IT Rémy JARDIN        */

int main() {
    int saisie, i;
    printf("Creation du Tableau. \nNombre de caractere du tableau : ");
    scanf("%d", &saisie);
    ArrayCreate(saisie);
     
    // Affichage
    
    return 0;
}

functions.h:

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

#include <stdio.h>
#include <stdlib.h>

int ArrayCreate(int saisie);
int randomizer();
int insereAIndice();

#endif

functions.c:

#include <stdio.h>
#include <stdlib.h>
    
int ArrayCreate(int saisie) {
    int i;
    int *Tab = (int *)malloc(saisie * sizeof(int));
    if (Tab == NULL) {
        printf("Not enough Memory");
        exit (1);
    }
    for (i = 0; i < saisie; i++) {
        Tab[i] = (randomizer + 1);
    }
    
    printf("\n Resultats : ");
    for (i = 0; i < saisie; i++) {
        printf("%d - ", *(Tab + i));
    }
    
    return 0;
}
    
int randomizer() {
    //int x = rand() % (100 + 1);
    return 1;
}

And the error is:

functions.c: In function 'ArrayCreate':
functions.c:12:8: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
 Tab[i] = (randomizer+1);

>Solution :

Instead of Tab[i] = (randomizer + 1); you should write:

Tab[i] = randomizer();

Note also these remarks:

  • the function prototypes in functions.h should have an argument of void:

    int randomizer(void);
    int insereAIndice(void);
    
  • file functions.c should include functions.h to ensure consistency between function declarations and definitions.

  • Writing *(Tab + i) is much less readable than Tab[i]. If you wish to obfuscate the code, use i[Tab] which is equivalent 🙂

Here is a modified version:

#include <stdio.h>
#include <stdlib.h>
#include "functions.h"

int ArrayCreate(int saisie) {
    int i;
    int *Tab = (int *)malloc(saisie * sizeof(int));
    if (Tab == NULL) {
        fprintf(stderr, "Not enough Memory\n");
        exit(1);
    }
    for (i = 0; i < saisie; i++) {
        Tab[i] = randomizer();
    }
    
    printf("\n Resultats : ");
    for (i = 0; i < saisie; i++) {
        printf(" %d", Tab[i]);
    }
    printf("\n");
    return 0;
}
    
int randomizer(void) {
    // return a random integer in the range 1..100
    return 1 + rand() % 100;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading