#creating a new variable

58 messages · Page 1 of 1 (latest)

raven radishBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

tame whale
#

lets say you write a function to "make a new account" and call it 2 times , the variables inside of the function are created 2 times and destroyed 2 times ( assuming you make simple non static variables)

malloc is for allocating memory on the heap . So if you want to store some data on the heap , you will need to use malloc. You would store data on the heap when you dont know the size of it at compile time. or you want to be able to resize things etc

leaden viper
#

it is malloc. the way it works is that you request some more memory and malloc returns a pointer to it

tame whale
#

;compile

#include <stdio.h>
void foo(int a){
  int x = a*2;
  printf("The double of %d is %d\n", a,x);
}

int main()
{
  foo(1);
  foo(2);
}
boreal zenithBOT
#
Program Output
The double of 1 is 2
The double of 2 is 4
tame whale
#

notice how in first case the value of x was 2 but in the next case it was 4

#

you dont have to "make new variables" to do that

#

now imagine instead of passing in int a , i pass in a string with the player's name. Then it can run the code and save that player's name to database

#

we never use malloc here

lilac matrix
#

can i post my code?

tame whale
#

yes , its best if you post the code and explain what u dont understand

leaden viper
# leaden viper it is malloc. the way it works is that you request some more memory and malloc r...

first you must specify the size of the data you want to create. for an integer you can use sizeof keyword to get its size and then pass it to malloc. then you can use the pointer that malloc returns like any normal pointer in C. lastly you'll need to free that allocated memory or you'll ran out of memory.

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

int main() {
    int *x = malloc(sizeof(int));
    free(x);
}
tame whale
#

you are still not "creating new variables" with malloc tho 😛

#

!format

raven radishBOT
#
int i;
int i2;
int i3;
int i6;

int inta;

scanf("%d", &inta);

int i4[inta][inta][inta][inta];

for (i = 0; i < inta; i++) {
  for (i2 = 0; i2 < inta; i2++) {
    for (i3 = 0; i3 < inta; i3++) {
      for (i6 = 0; i6 < inta; i6++) {
        i4[i][i2][i3][i6] = i + 1;
        printf("[%d][%d][%d][%d][%d] ", i, i2, i3, i6, i4[i][i2][i3][i6]);
      }
      printf("\n");
    }
    printf("\n");
  }
  printf("\n");
}

So, this is the var: i4[i][i2][i3][i6]

I want to make an individual amount of variables(not array) and set them to any amount of 'scanf("%d",&inta);' that is imputed

WaterSerpentM
lilac matrix
#

if that makes sense

#

so, from i4[i][i2][i3][i6] to individual ints

tame whale
# lilac matrix so, from i4[i][i2][i3][i6] to individual ints
#include<stdio.h>
int main()
{
  constexpr int length = 5;
  int arr[length] = {};

  for(int i=0; i<length; i++)
  {
    printf("Enter %dth int : \n", i);
    scanf("%d",&arr[i]);
  }

  for(int i=0; i<length; i++)
  {
    printf("%dth number = %d\n",i ,arr[i] );
  }
}

i think this is what u want ?

#

basically if you want to store 10 ints , instead of making 10 individual variables, you make an int array of lenght 10

#

so you can store 10 ints under 1 name

#

btw if u dont understand

  constexpr int length = 5;

just assume its like (in reality , its really not, but u can assume they will do the same thing for now )

#define length 5
violet rock
#

note that this is C23 and will not be accepted by some compilers and might not be recognized as valid C by some instructors

violet rock
#

constexpr, = {}

tame whale
#

={} is C23 ? damm

violet rock
#

there's = {0};
which is the common way of zeroing

lilac matrix
#

can it be this amount of length

( inta * inta * inta * inta)

tame whale
#

sure , but that looks like a pretty big number
so if inta is like 4 , you are making an array of length 256

#

can you explain what you are trying to do ?

lilac matrix
#

ye because i want to fill the array to the length

tame whale
violet rock
#

oh and also allocating a huge number of ints on stack (inside main fn) will cause a stack overflow

#

you either do malloc or static array

tame whale
#

yeah , if its a HUGE array or if you dont know the size of this array at compile time , you can allocate it on heap at runtime

lilac matrix
violet rock
#

it doesn't have to be that huge really.
1 mb is enough to stack overflow. and note that you won't be notified that you got a stack overflow. your program will run in a random way

tame whale
lilac matrix
#

yes

violet rock
#

basically you need to do malloc

tame whale
#

can you give an example with numbers ?
also its very rare to see a 4 dimensional array , at least its my first time seeing it

lilac matrix
#

ye was just messing about

urban oak
#

you could just do a function translating 4d array to 1d array

lilac matrix
#

so, if ``` scanf("%d",&inta);

    int i4[inta][inta][inta][inta];```

inta is inputed as 2,

then int i4 multiplies to 16 variables

raven radishBOT
#

@lilac matrix

It looks like you may have code formatting errors in your message

Note: Make sure to use back-ticks (`) and not quotes (')
Note: Make sure to specify a highlighting language, e.g. `cpp`, after the back-ticks

Markup

```c
int main() {}
```

Result
int main() {}
lilac matrix
tame whale
#

if you want to allocate a 4 dimensional array on the heap of i4[i][i2][i3][i6] these dimensions , the process... is a bit involved
you first allocate an array of size i , and each element of this array is a pointer to pointer to pointer to int int ***
then you loop through each of these and then allocate an array of size i2 , each element of this array is int **
then you loop through each of these and then allocate an array of size i3, each element of this array is int*
then you loop through each of these and then finally allocate an array of size i6 , each element of this array is int

tame whale
#

allocate a big 1D array of i * i2 * i3 * i6 and then make an interface to view it as a 4D array

urban oak
lilac matrix
#

i did mention for it not to be an array, my project is for it to be individuel INT not array

tame whale
#

int array is just a container to store lot of ints

#

instead of making 216 individual variable names ,you can just make 1 variable and store all ints in it

lilac matrix
#

bit of a problem

        int *mmm = malloc(inta*inta*inta*inta(int))```
raven radishBOT
#

@lilac matrix

It looks like you may have code formatting errors in your message

Note: Make sure to use back-ticks (`) and not quotes (')
Note: Make sure to specify a highlighting language, e.g. `cpp`, after the back-ticks

Markup

```c
int main() {}
```

Result
int main() {}
lilac matrix
#

any help?

#
int number[inta*inta*inta*inta] = {0};
int *mmm = malloc(inta*inta*inta*inta(int));```
lilac matrix
#

int number[intaintaintainta] = {0};
int mmm = malloc((intainta
inta*inta) * sizeof(int));