0

Still teaching myself C. This time, I'm trying to read a text from the keyboard using a function:

int read_line(char my_string[]) {
    int characters;
    fgets(my_string, sizeof(my_string), stdin);
    characters = strlen(my_string);

    printf("characters =%d, my_string=%s. \n", characters , my_string);
}

int main(int argc, char *argv[]) {
    char line[MAX];
    read_line(line);
}

When I enter in keyboard this:

abcdefg

I get this on the console:

characters =3, my_string=abc.

Why is C behaving in that way? What's the proper way to read a character string through a method?

  • 2
    Please get a book to learn from, it will explain how to pass arrays to functions and a lot of other fundamentals. – M.M Sep 23 '14 at 04:05
  • 1
    "Still teaching myself C" -- if you mean trying things randomly to see if they work, that's a bad idea, and leads to things like `sizeof(my_string)` when `my_string` is a pointer (it looks like an array, but isn't -- one of the warts of C). – Jim Balter Sep 23 '14 at 04:52

2 Answers2

4

You're using sizeof in your fputs. C can't determine the size of the buffer that way. To further explain, sizeof is used to determine the SIZEOF a type.

Example:
sizeof(short) = 2
sizeof(long) = 4
sizeof(char) = 1 (generally speaking, unless you compile for UNICODE)

Reason the original code did not work: When you use char[] as a parameter, it gets turned into a char * (a pointer to a char[]) which is why it has a sizeof of 4 (pointers are generally 4 bytes). If you use sizeof(line) in main() it will report the size properly due to the compiler changing the code to reflect that information at compile time.

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
int read_line(char my_string[]);

#define MAX 10

int _tmain(int argc, _TCHAR* argv[])
{
    char line[MAX];
    read_line(line);
    return 0;
}

int read_line(char my_string[]) {
    int characters;
    fgets(my_string, MAX, stdin); <--FIX here
    characters = strlen(my_string);

    printf("characters =%d, my_string=%s. \n", characters , my_string);
    return 0;
}
Thomas
  • 1,401
  • 8
  • 12
  • You never mention what type `sizeof(my_string)` determines the size of or what size that is ... – Jim Balter Sep 23 '14 at 04:53
  • Bit advanced there, but sizeof(my_string) would be the size of a pointer. Since when you use char[] as a parameter, it gets converted to a pointer. – Thomas Sep 23 '14 at 04:58
  • Yes, I know that ... you should add it to your answer. (SO encourages people to add all relevant information to their answers rather than just having it in comments.) Advanced or not, it's fundamental to what's going on here, and would make your good answer even better. – Jim Balter Sep 23 '14 at 05:05
3

WRONG:

 int read_line(char my_string[]) {
 int characters;
 fgets(my_string, sizeof(my_string), stdin);

Try "printf ("sizeof my_string[]= %d\n", my_string);

I'll bet you get "4".

Which is the problem.

Instead, specify your buffer size, e.g. "80". Or whatever you've allocated your buffer to be.

EXAMPLE:

int read_line(char my_string[]) {
    int characters;
    fgets(my_string, MAX, stdin);
    ...
FoggyDay
  • 11,962
  • 4
  • 34
  • 48
  • *"printf ("sizeof my_string[]= %d\n", my_string); I'll bet you get "4".* -- I'll bet not. Please don't write gibberish in your answers – Jim Balter Sep 23 '14 at 04:55