The following C Code gives a segmentation fault:
#include <stdio.h>
#include <stdint.h>
int main(){
uint32_t *a;
uint32_t idx=1233245613;
a[idx]=1233;
return 0;
}
How can I use uint32_t as index of an array in C? Or how can I use array like structure which can get uint32_t and 12 digit numbers as an index?
I'd appreciate any kind of help.
Also you are trying to index pretty far in the array. You may not have enough memory available for this so be sure to check for NULL.
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
int main(void){
uint32_t *a;
uint32_t idx=1233245613;
//This allows you to index from 0 to 1233245613
// don't try to index past that number
a = malloc((idx+1) * sizeof *a);
if(a == NULL)
{
printf("not enough memory");
return 1;
}
a[idx]=1233;
free(a);
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With