Read a string char by char from an associative pointer array

I hope the title of my question is correct.

I have a stringpool

char **string_pool;

that gets initiated in a function like

string_pool = malloc( sizeof(char *) * 1024);

i get strings from stdin and scanf them into the array

scanf("%[^\n]s", &string_pool[index]);

so i can print it out using printf

printf("gets %s\n", &string_pool[index]);

how can i

  • get the length of string_pool[index]
  • read string_pool[index] char by char in a loop

Thank you

Edit

Maybe i should explain it a bit more, its a virtual machine with a virtual instruction set and a program like

push 1
read
gets

should :

  • push 1 on the stack -> let x be 1
  • read stdin as string into string_pool[x]
  • push all characters onto the stack

the functions looks like

    case GETS: {
        int index = popv(); // index is already on top of the stack
        int strl = strlen(&string_pool[index]);
    printf("gets %s with a length of %d\n", &string_pool[index], strl);
    // pseudo code
    // push each char as integer on the stack
    foreach(char in string_pool[index]) push((int)char);

    break;
}

case READ: {  
    int index = popv();          
    scanf("%[^\n]s", &string_pool[index]);
    break;
}

case WRITE: {  
    int index = popv();          
    printf("%s", &string_pool[index]);
    break;
}

My problem is in the GETS case. I want to push every char as int onto the stack.

#c #arrays #string

3 Likes2.45 GEEK