I hope this helps you.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#define Y 30 //This is unused
int nrreturn();
int main(int argc, char **argv) {
char **feld;
int i, j, z = 0; //it is dangerous to use an uninitialized variable, z could be set to a very high value, causing no loop or a very long loop based on sign.
feld = (char **)malloc(sizeof(char *) * nrreturn());
//for(i = 0; i < Y; i++) { //Y = 30, but feld only has enough memory allocated for nrreturn() elements
for(i = 0; i < nrreturn(); i++) {
feld[i] = (char *)malloc(sizeof(char) * nrreturn());
}
//This has the same problem, you've only allocated 5 elements of 5 char's each, but you're assigning 30 elements of 30 char's.
/*for(j = 0; j < Y; j++) {
for(i = 0; i < Y; i++) {*/
for(j = 0; j < nrreturn(); j++) {
for(i = 0; i < nrreturn(); i++) {
feld[j][i] = 0;
}
}
for(j = 0; j < nrreturn(); j++) {
//for(i = 0; i < Y; i++) { //same
for(i = 0; i < nrreturn(); i++) {
printf("%d",feld[j][i]);
}
//why are we doing this?
while(z<1000) {
z++;
}
z = 0;
free(feld[j]);
printf("\n");
}
free(feld);
return 0;
}
int nrreturn() {
return 5;
}
printf("%d", some_char) has worked forever, and I believe that printf() properly promotes the char to an int when used in this way. If you believe otherwise, you can just cast the char to an int.
Assuming you're using Visual Studio, go to the properties page for your project, navigate to C/C++ -> Advanced, locate the Compile As property and change it to Compile as C. Otherwise just call this code C++, so no one gets confused.