Thread: I don't understand K&R example

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It's the same thing as the strcpy() function:
    Code:
    #include <string.h>
    
    int main(void)
    {
      char str[50];
    
      strcpy(str, "Hello, world!");
      return 0;
    }
    See? str's contents are not defined when you pass str to strcpy(). That's because strcpy() fills the array! If the function is meant to accept a string and then manipulate the data in some way then you'd need to fill str with something meaningful before you used it. For example:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char str[50];
    
      puts(str);
      return 0;
    }
    puts() isn't designed to accept an undefined string so you'll just get garbage output. So there's no universal "You don't have to fill the character array before passing it to a function" rule. It depends on what the function expects.
    If you understand what you're doing, you're not learning anything.

  2. #2
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22

    thanks

    Thanks, itsme86. Your last post to this thread clarified the point I had a doubt about.
    That's all. Just wanted to thank you.

    Roberto.

Popular pages Recent additions subscribe to a feed