Live data from Hacker News

Begginer in C. Critique my code and give ideas?

github.com

1–4 of 4 posts

Re: Begginer in C. Critique my code and give ideas?

#3
post #2

Hello HN. I'm trying to learn / relearn C (for real this time). Wrote this little tool for linux and I would like some feedback. Also, if you could give me some ideas for future small projects that would give me more experience in C... thanks!

  int get_number(char buffer[])
  {
    long int i = 0;
    char *p;
    if(fgets(buffer, sizeof(buffer), stdin) != NULL)
  {
sizeof(buffer) will always be 4 (or 8 if you compile 64 bit) because sizeof(pointer to char) is 4 (or 8). It will NOT be the size of the buffer that you pass as an argument. To fix this problem you need to change the function to

  int get_number(char buffer[], int buffer_size)
Also the buffers that you pass to this function seem to be too small.

Re: Begginer in C. Critique my code and give ideas?

#4
post #3
post #2

Hello HN. I'm trying to learn / relearn C (for real this time). Wrote this little tool for linux and I would like some feedback. Also, if you could give me some ideas for future small projects that would give me more experience in C... thanks!

int get_number(char buffer[]) { long int i = 0; char *p; if(fgets(buffer, sizeof(buffer), stdin) != NULL) { sizeof(buffer) will always be 4 (or 8 if you compile 64 bit) because sizeof(pointer to char) is 4 (or 8). It will NOT be the size of the buffer that you pass as an argument. To fix this problem you need to change the function to int get_number(char buffer[], int buffer_size) Also the buffers that you pass to th…

thank you very much. i appreciate it!