0

How can I fill an empty Char Array with keyboard? something like

char a_string[];
while("if not Q")
{
printf("Enter a number: ");
scanf("%c", a_string);
}

I know this is wrong I just want to know how to give values to my a_string[], without limiting the size. so the size will vary depend on how many keys i'm gonna enter from keyboard.

Thanks!

ericDaWang
  • 182
  • 2
  • 6
  • 15

3 Answers3

0

Try this:

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

int main() {
    char *string = NULL;
    char *newstring = NULL;
    char c = '\0';
    unsigned int count = 0;

    while(c != 'Q'){
        c = getc(stdin);
        if(string == NULL){
            string = (char *) malloc(sizeof(char)); // remember to include stdlib.h
            string[0] = c;
        }
        else{
            newstring = (char *) realloc(string, sizeof(char)*count);
            string = newstring;
            string[count] = c;
        }
        count++;
    }

    string[count-1] = '\0';  // remove the Q character
    fprintf(stderr,"here you are: %s",string);
    free(string); // remember this!
    return 0;
}
HAL9000
  • 3,562
  • 3
  • 25
  • 47
0

If you will know at the start of runtime how many keys you'll enter, you can have it ask first for the number of keys and then for the individual characters, as in the untested snippet below.

Otherwise, you have to set some real-world maximum (e.g. 10000) that will never be reached, or, if that's not possible, set a per-array maximum and make provisions for rollover into a new array. That last option really is the same (eventually bounded by memory) but gives you a larger maximum.

char *mychars;
int numchars;

printf("Please enter the total number of characters:\n");
if (scanf("%d", &numchars) == NULL) {
  printf("couldn't read the input; exiting\n");
  exit(EXIT_FAILURE);
}

if (numchars <= 0) {
  printf("this input must be positive; exiting\n");
  exit(EXIT_FAILURE);
}

mychars = (char *) malloc (numchars * sizeof(char));

int current_pos = 0;
printf("Enter a digit and hit return:\n");

while (scanf("%c", &mychars[current_pos]) != NULL && current_pos < numchars) {
  current_pos++;
  printf("Enter a digit and hit return:\n");
}
kbshimmyo
  • 578
  • 4
  • 13
0

Repetitive calls to realloc() will meet the need.

Double realloc() size as needed to avoid O(n) calls.

char *GetQLessString(void) {
  size_t size_alloc = 1;
  size_t size_used = size_alloc;
  char *a_string = malloc(size_alloc);
  if (a_string == NULL) {
    return NULL; // Out  of memory
    }

  char ch;
  while(scanf("%c", &ch) == 1 && (ch != 'Q')) {
    size_used++;
    if (size_used > size_alloc) {
      if (size_alloc > SIZE_MAX/2) {
        free(a_string);
        return NULL; // Too big - been typing a long time
      }
      size_alloc *= 2; 
      char *new_str = realloc(a_string, size_alloc);
      if (new_str == NULL) {
        free(a_string);
        return NULL; // Out  of memory
      }
      a_string = new_str;
    }
    a_string[size_used - 2] = ch;
  }

  a_string[size_used - 1] = '\0';
  return a_string;
}

Code could do a final realloc(a_string, size_used) to trim excess memory allocation.
Calling routine needs to call free() when done with the buffer.
The following would be cleaner.

int ch;
while((ch = fgetc(stdin)) != EOF && (ch != 'Q')) {
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256