Help me find out what's wrong

This is a discussion on Help me find out what's wrong within the C Programming forums, part of the General Programming Boards category; I'm an amateur programmer, bare with me: I am trying to write my own strcat() function and testing it. Here ...

  1. #1
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230

    Help me find out what's wrong

    I'm an amateur programmer, bare with me:

    I am trying to write my own strcat() function and testing it. Here is the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <malloc.h>
    
    int stringcat(char *s, char *t);
    
    int main(void) {     
          
          char *ptr1;
          char *ptr2;
          
          ptr1 = (char *)malloc(20 * sizeof(char));
          ptr2 = (char *)malloc(20 * sizeof(char));    
                
          char message1[] = "Please ";
          char message2[] = "Reconsider\n";
          
          strcpy(ptr1, message1);
          strcpy(ptr2, message2);
          
          stringcat(ptr1, ptr2);
                   
          while (*ptr1 != '\0')
                printf("%c", *ptr1++);
                
          free(ptr1);
          free(ptr2);
          
          return 0;
          
    }
    
    /* Copies string T to the end of string S */
    int stringcat(char *s, char *t)
    {
         while (*s++ != '\0')
               ;
         *s--;
         
         while ((*s++ = *t++) != '\0')
               ;
         *s = '\0';
         
         return 0;
    }
    I keep getting a parse error around the place where I declare/initialize the arrays (message1 & message2).

    Can anybody help me with this? The code looks fine!
    Last edited by biosx; 08-15-2001 at 01:29 AM.

  2. #2
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Code:
    // PracticeWorkspace.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <stdio.h>
    #include <string.h>
    #include <malloc.h>
    #include<conio.h>
    
    void stringcat(char *s, char *t);
    
    int main(void) {     
          
        char *ptr1 = (char *)malloc(20 * sizeof(char));
    	memset(ptr1,'\0',20);
        char *ptr2 = (char *)malloc(20 * sizeof(char));    
                
        char message1[] = "Please ";
        char message2[] = "Reconsider\n";
          
        strcpy(ptr1, message1);
        strcpy(ptr2, message2);
         
        stringcat(ptr1, ptr2);              
    
    	printf("%s",ptr1);
    	
    	_getch();  
        free(ptr1);
        free(ptr2);
    
        return 0;
          
    }
    
    /* Copies string T to the end of string S */
    
    void stringcat(char *s, char *t)
    {
    
    	while (*s != '\0') s +=1;
    
    	while (*t != '\0')
    	{
    	
    		*s = *t;
    		s += 1;
    		t += 1;
    	}
    
    }
    Reminder to self. I must get used to this new board!
    Last edited by Witch_King; 08-15-2001 at 04:15 AM.
    I compile code with:
    Visual Studio.NET beta2

  3. #3
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    You're problems where here:

    Code:
    //no good because it advances the
    //pointer, so that free() can not
    //free the space properly
    
    while (*ptr1 != '\0')
      printf("%c", *ptr1++);
     
    //use printf("%s",ptr1); instead
    Also the other error
    Code:
    //should just be s--;            
      *s--;
    //and this is not an error
    *s = '\0';
    //but could have been writen as
    *s = *t;
    //either way
    What I'm saying here is use your code and just make these changes. It works too.
    Last edited by Witch_King; 08-15-2001 at 04:33 AM.
    I compile code with:
    Visual Studio.NET beta2

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > I keep getting a parse error around the place where I declare/initialize the arrays (message1 & message2).
    Because you're writing C++, and compiling it as C.

    C does not allow declarations at statements to be mixed - you must declare all your variables before the first statement in any {} block.

    So change them over like this
    Code:
          char message1[] = "Please ";
          char message2[] = "Reconsider\n";
          ptr1 = (char *)malloc(20 * sizeof(char));
          ptr2 = (char *)malloc(20 * sizeof(char));
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Originally posted by Salem
    > I keep getting a parse error around the place where I declare/initialize the arrays (message1 & message2).
    Because you're writing C++, and compiling it as C.

    C does not allow declarations at statements to be mixed - you must declare all your variables before the first statement in any {} block.

    So change them over like this
    Code:
          char message1[] = "Please ";
          char message2[] = "Reconsider\n";
          ptr1 = (char *)malloc(20 * sizeof(char));
          ptr2 = (char *)malloc(20 * sizeof(char));
    Oh my god, it worked. I never knew that. I always mix them, especially with a for() loop. I usually declare i right before the loop.

    Thank you so much for clearing that up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linux find utilty
    By vaibhavs17 in forum Tech Board
    Replies: 5
    Last Post: 05-12-2009, 04:40 AM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. Whats wrong with my find Circle Area program?[compiles fine]
    By Golden Bunny in forum C++ Programming
    Replies: 22
    Last Post: 06-16-2002, 02:49 PM
  4. Help!!!!!!!! What Am I Doing Wrong????
    By Cnote in forum C++ Programming
    Replies: 6
    Last Post: 05-19-2002, 10:09 PM
  5. Help i cant find whats wrong with my code
    By 0927 in forum C++ Programming
    Replies: 1
    Last Post: 12-21-2001, 08:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21