I have written some code in C by taking the maximum size of char array as 100. It worked well. But when I increase the maximum size of char array to 10000 it gives me segmentation fault(as it has exceeded its limit). Can someone tell me how can I increase the maximum size and store a string of length 10000.
i.e How can I take the "char a[100]" as "char a[10000]" and execute the same code????
You can allocate the array dynamically:
and when you want to increase its size:
Eventually, remember to free the allocated memory, when the array is not needed anymore:
Basically
realloc(prt, size)
returns a pointer to a new memory block that has the size specified bysize
and deallocates the block pointed to byptr
. If it fails, the original memory block is not deallocated. Please read here and here for further info.