0

I am trying to input string into fixed size char array. I have a questions:

  1. when I input a string which is bigger than the char array, the array become bigger without any additional declaration. I want to make the code only take the string that 'equal or smaller than the char array'.

Thank You.

CODE:

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

/***************************Function****************************/
int string_length(char s[]) {
    int c = 0;

    while (s[c] != '\0')
        c++;

    return c;
}
/**************************************************************/
char *str;
int arrSize;

void opt1()
{
    printf("Enter array size: ");
    scanf("%d", &arrSize);
    arrSize=arrSize+1;
    str = malloc(arrSize);
    return;
}

void opt2()
{
    printf("Enter characters: ");
    scanf("%s", str);

    length = string_length(str);

    printf("your input is '%s'\n", str);
    printf("your input length is '%d'\n", length);

    return;
}

int main()
{
    int input = 0;

    while(input != 3) {
        printf("\n NAME \n");
        printf("\n");

        printf("--------------------------------------\n");
        printf("1) Making Array \n");
        printf("2) Check Array \n");
        printf("3) Quit\n");
        printf("\nEnter selection: ");
        scanf("%d", &input);

        if( input == 1 ) {
            /* */
            opt1();
        }
        else if(input == 2) {
            opt2();
        }


    }
    return 1;
}
16
  • 4
    1) scanf("%s\n", &str); --> scanf("%s", str); Commented Nov 1, 2015 at 20:05
  • 1
    type of str is char *. (see char *str;). your actual code char str;, not char *str;. Commented Nov 1, 2015 at 20:22
  • 1
    see my TEST CODE Commented Nov 1, 2015 at 21:00
  • 2
    I put more characters than array size it also take it. this is undefined behavior. Possibly unintended destruction of the memory takes place. It may not discovered immediately if there even if the cause is discovered to segment fault immediately. Commented Nov 1, 2015 at 21:25
  • 1
    use realloc. memory block that has been secured by malloc can be extended by realloc. Commented Nov 1, 2015 at 21:55

2 Answers 2

2

OP wants to read data, yet if larger that the target array, then do not change the target array.

// 1: success
// -1 EOF
// 0: Input too long
int read_array(char *buffer, size_t size) {
  char tmp[size];
  size_t i = 0;
  int ch;

  while ((ch = fgetc(stdin)) != EOF && ch != '\n') {
    if (i < size) { 
      tmp[i++] = ch;
    }
  }

  if (ch == EOF && i == 0) return EOF;
  if (i >= size) return 0;  // too many
  memcpy(buffer, tmp, i);
  buffer[i] = '\0';
  return 1;
}     

Normally code could use fgets(), but there are corner cases that fail to meet OP goals.

Sign up to request clarification or add additional context in comments.

4 Comments

I am not sure how to use this function, so when i enter the string how it make it as a string variable and send to the 'read_array' ? use malloc?
@IAMBEAST Say p is your "fixed size char array" and L is its size, then int result = read_array(p, L);. Test result for -1, 0,1 and the act per EOF, too long an input, just right
memcpy(buffer, tmp, i); buf[i] = '\0'; return 1; it won't work, if you dont declare
@IAMBEAST Changed to memcpy(buffer, tmp, i); buffer[i] = '\0';
1

To read in a whole line, you can use fgets:

char line[80];

if (fgets(line, sizeof line, stdin) != NULL) {
    // use the input
}

Now you won't need to check if the user entered more than the limit, since fgets will only get the first 79 (-1 for null terminator) characters; the remainder, if any, will be ignored.

4 Comments

The statement the remainder, if any, will be ignored. is not correct. fgets reads 78 characters and add a termination 0 character. It does not ignore remaining part of the line, which will be read in the next fgets, if any.
The -1 advice is not needed. fgets(line, 80, stdin) is fine, fgets(line, sizeof line, stdin) is even better.
but I want to make it as not accepting the input if it is bigger than size of array. so if the input is bigger it should not make an array, not taking the part of input.
You should use 80 rather than 79 , otherwise you are just wasting an allocated character

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.