Thread: strcat program

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    9

    strcat program

    i read C Programming Language chapter 2-2.8 and wrote a strcat program
    Code:
    #include <stdio.h>
    
    void cat(char s[],char t[]);
    
    main()
    {
    
    char s[]="Hello ";
    char t[]="World";
    
    printf("%s\n",cat(s,t));
    
    }
    
    void cat(char s[],char t[])
    {
    int i,j;
    i=j=0;
    
    while(s[i] != '\0')
    i++;
    while((s[i++]=t[j++]) != '\0');
    
    }
    when i try to compile using gcc i get
    In function `main':
    11: invalid use of void expression
    Why is that invalid?
    then i replace void cat(... with int cat(... and it compiles but when i run it , it prints crap like
    42@øÿ¿Døÿ¿ÌH@î@World

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    >main()
    Should be int main(void)

    >printf("%s\n",cat(s,t));
    This means you want to print the string that cat returns, but you have cat set to return void

    >void cat(char s[],char t[])
    This should be char* vat(char[], chart[])

    Then you need to return s at the end of cat

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    9

    Talking

    thanks a lot , it works fine now

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    I'm sure that it appears to work just fine, but you still have a rather serious problem. Turn back to the last paragraph on the previous page (47) which says in part: "strcat assumes that there is enough space in s to hold the combination." Is there?

    Hint: how large is the array 's' in main? How big does an array need to be to hold "Hello World" plus a '\0' terminator?

    HTH,
    Will

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM