1

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.

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);
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • `randomizer` is a function. You use it as a variable. You need to call the function. – Some programmer dude May 26 '22 at 23:30
  • On another (but unrelated) note, in C you [don't have to (and really shouldn't) cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/). – Some programmer dude May 26 '22 at 23:31
  • Oh, and don't `#include` source files. If you want a project with multiple source file you should build them one by one and then *link* them together into the executable program file. – Some programmer dude May 26 '22 at 23:33
  • All in all, with the problem you have, the mixed use of array indexing (as arrays and as pointers) including a source file, the casting of the `malloc` result, and a couple of other minor details, it makes me suspect you're not using a very good source to learn C. What resources *are* you using? – Some programmer dude May 26 '22 at 23:34
  • Thank you ! However, what do you mean by "don't #include source files" ? – Rémy jardin May 26 '22 at 23:35
  • You have `#include "functions.c"`. That's not the correct way to do projects using multiple source files. – Some programmer dude May 26 '22 at 23:36
  • Oh I see, I should use a makefile right ? – Rémy jardin May 26 '22 at 23:37
  • @Rémyjardin: so the argument is *readability*... The most important here is consistency. You can either use `int *Tab = (int *)malloc(saisie * sizeof(int));` or `int *Tab = malloc(saisie * sizeof(*Tab));` The latter is simpler and does not need to be modified if you later change the type of `Tab`. – chqrlie May 26 '22 at 23:46
  • With larger, projects tools similar to `make` are really needed. There are meta-build tools which can generate makefiles (or files for other build-systems) for you, like CMake and Meson. – Some programmer dude May 27 '22 at 05:25

1 Answers1

1

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;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189