I am making a program that asks me for various data, one of them is how many companies I want to work with, then the user enters a number that is stored in the cantEmpresas variable, which I then use in a for, but it is not arriving correctly and no I am able to visualize where the problem is, I appreciate your help
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MESES 12
void cargarNombreYApellido (char nombre[100]);
void crearContrasenia (char nombre[100], char password[200]);
int elegirCantidadDeEmpresas(int cantEmpresas);
void cargarMatrizAleatoria(int cantEmpresas, int empresas[][MESES]);
//#############################################################
int main()
{
int cantEmpresas;
elegirCantidadDeEmpresas(cantEmpresas);
int empresas[cantEmpresas][MESES];
cargarMatrizAleatoria(cantEmpresas, empresas[cantEmpresas][MESES]);
return 0;
}
//##############################################################
int elegirCantidadDeEmpresas(int cantEmpresas){
printf("\n\n\nCon cuantas empresa deseas trabajar? (Menos de tres) \n");
scanf("%d", &cantEmpresas);
//agregar problema al añadir 3 empresas o mas
}
//En este for no llega el cantEmpresas, si funciona cuando introduzco otro numero
void cargarMatrizAleatoria(int cantEmpresas, int empresas[cantEmpresas][MESES]){
int i;
for (i=0; i<cantEmpresas; i++){
printf("f");
}
}
Thank you very much
Your problem is that you pass the parameter by value and not by pointer. Let's go to the basics:
In c there are two ways to pass parameters to a function. By value and by pointer. (Also in C++ you could pass the value by reference ).
When you pass parameters by value, in reality the program makes, behind the scenes, a copy of the variable, in such a way that any modification made to it within the routine will not be reflected in the variable with which the call was made. .
When you pass a parameter by pointer (or by reference), instead, you pass "the variable itself"—actually, its memory address—and work on that same memory space so that any changes made within the routine it will be reflected in the variable that was used to call it.
In modern compilers, you can use the operator
&
in the function declaration, to pass by reference.In other words, to pass the parameter by reference, in the function declaration, change to something like:
And the rest of the program would not suffer any change.
In classical standard C, however, you must use pointers, i.e. change the declaration and also the way the parameter is used and the function is called:
And the call changes to:
That said, there is a major inconsistency in your code: your function, according to its declaration, returns an integer (which could be the number of companies). I suggest you do this, or change your function declaration to return nothing (
void
):If you decide on the first, everything is simplified, since there are no more parameters:
And you use it like this:
If you have already created a function that returns an integer, it is unnecessary to pass a variable as a parameter, take advantage of the fact that the function already returns a value to return the number of companies:
And in your main you call it like this: