Here is the code:
I'm trying to write a program and running into some problem with the array.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char ** argv)
{
unsigned long numberN = strtoul(argv[1],NULL,10);
char** storage = (char**) malloc(10*sizeof(char*));
char* current = calloc((numberN+1),sizeof(char*));
int item = 0;
int c;
while (c != EOF)
{
int n = 0;
while (n < numberN && c != EOF)
{
c = getchar();
if (c == EOF)
current[n] = '\0';
else
current[n] = c;
n++;
}
storage[item] = current;
char a = '0' + item;
putchar(a);
putchar(storage[item][0]);
putchar(storage[0][0]);
item++;
}
}
Output: 0aa1bb2cc3dd4
when input is abcd and numberN is 1.
Basically, "item" is supposed to increment and as shown by the output it is incrementing but somehow the information is still being stored in storage[0] when item is no longer 0.
Someone please tell me what is wrong with my code. Much appreciated.