char+string

This is a discussion on char+string within the C Programming forums, part of the General Programming Boards category; is there any way to add a character to a string in a direct way ? I mean without changing ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    1

    char+string

    is there any way to add a character to a string in a direct way ? I mean without changing it to a pointer , ... just concat it right away ... because I wrote a code, it works in my test program but when I add it up to the main program it crashes ...
    Code:
    char* analyseCommand(char *inputCom){
            char text[200];
            char temp[50];
            char *param[5];
            int j=0 , i=0;
            printf("\nthis is input %s\n",inputCom);
            strcpy(text,inputCom);
            printf("\n this is text %s\n",text);
            char *ptr =  (char*) malloc(sizeof(char));
            exit(0);
            strcpy(temp,"");
            printf("%c\n", text[0]);
            while (text[j] != '\0'){
                    if (isspace(text[j]) == 0){
                            printf("%c\n", text[j]);
                            *ptr = text[j];
                            strcat(temp,ptr);
                    }//end of if
                    else{
                            printf("WOW WOW WOW  %s\n", temp);
                            param[i] =  (char*) malloc(sizeof(char));
                            strcpy(param[i],temp);
                            printf("WOW WOW WOW  %s\n", param[i]);
                            i++;
                            strcpy(temp,"");
                    }//end of else
                    j++;
            }//end of while
            param[i] =  (char*) malloc(sizeof(char));
            strcpy(param[i],temp);
            printf("WOW WOW WOW  %s\n", param[i]);
            i++;
            strcpy(param[i],NULL);
            printf("WOW WOW WOW  %s\n", param[i]);
    
    }//end of analyseCommand()

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I counted about 10 buffer overflows (or potential ones ).
    And about 10 lines of pointless, broken code. Plus a few segfault lines

    Also, sizeof(char) == 1 (ALWAYS).

    The list of your problems is huge, check the FAQ out -- which should solve a few, then perhaps get a book (learn to walk before you run).

    [edit]
    Code:
    char text[200];
    /* ... */
    strcpy(text,inputCom);
    printf("\n this is text %s\n",text);
    The array is uninitalized, do you know how strcpy() works? Look it up, it's going to be a problem is text[] contains no 0-bytes, isn't it? One of your crashes comes from this line. If it doesn't, consider yourself VERY unlucky.

    [/edit]
    Last edited by zacs7; 10-14-2008 at 01:40 AM.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    You can append to a string - provided there is sufficient storage available.
    A string works fine even if you have a lot of unused space inside it. In other words, you don't need exactly 6 bytes to store "Hello". It can 50 bytes or even more, if you want.

    And for the sake of the gods, do not forget to free what you malloc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM

Tags for this Thread


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