Thread: Strings are not concatenating according to my understanding.

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    8

    Strings are not concatenating according to my understanding.

    So the point of this code, was to take a number and then that number will mean you are allowed to concatenate a number of string so to the original string.

    It worked!!

    But, not as well as I'd hoped.

    There is a TEENY tiny bug in this code, for example if i type the number five and for my strings I type the strings "The Friends of Ringo Ishikawa"

    I get TheFriendsOfRingoIshikawa as intended, but before that comes these this weird character and an a. So, if I type in the number 5 when prompted and then The Friends of Ringo Ishikawa I get the output: ö aTheFriendsOfRingoIshikawaIshikawa

    What happened?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <stdarg.h>
    
    
    char*variable_string(int num, char*a, ...);
    
    
    int main()
    {
    
    
      int num = 0;
      char*string = (char*)malloc(sizeof(char)*100);
      string[100] = '\0';
      printf("Please enter the number of strings you want to add to the string.");
      scanf("%d", &num);
      printf("\n num is %d", num);
      printf("\nNow please enter the strings you wish to involve in your char");
      for(int i = num; i > 0; i--)
      {
        printf("\nnum is %d", i);
        //fgets(string, sizeof((char*)string), stdin);
        scanf("%s", string);
        /*printf("\n%s", string);*/
        variable_string(num, string);
      }
    
    
      //variable_string(num, string);
      printf("\nand the string is %s", variable_string(num, string));
      return 0;
    }
    
    
    char*variable_string(int num, char*x,...)
    {
      va_list arg_ptr;
      char*a = (char*)x;
      va_start(arg_ptr, x);
      char*b = va_arg(arg_ptr,char*);
      strcat((char*)b, (char*)a);
    
    
      va_end(arg_ptr);
    
    
      //printf("%s", (char*)b);
      return (char*)b;
    }
    Last edited by MATT BOOTY; 06-14-2019 at 11:28 AM. Reason: Replace old code with working code

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In main, you need two strings: one string to store ths overall result and one string to store the string read from the user.

    Note that you aren't actually making use of varargs in this way: you should ditch both the loop and user input and manually test with something like variable_string(2, result_string, "hello", "world"). Otherwise, you're just writing a complicated wrapper for strcat that ultimately merely calls strcat.
    Last edited by laserlight; 06-14-2019 at 11:23 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2019
    Posts
    8
    Thank you laserlight

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Remember that string[100] is the 101st element, not the 100th.

  5. #5
    Registered User
    Join Date
    Jun 2019
    Posts
    8
    Quote Originally Posted by Click_here View Post
    Remember that string[100] is the 101st element, not the 100th.
    Alright, but why does assigning a null character at the 101st element even remotely have anything to do with those bizarre characters at the front of the string?

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by MATT BOOTY View Post
    Alright, but why does assigning a null character at the 101st element even remotely have anything to do with those bizarre characters at the front of the string?
    It doesn't - I just saw an easy to fix bug and told you that it was there because I'm a nice person.

  7. #7
    Registered User
    Join Date
    Jun 2019
    Posts
    8
    Wait, how is that a bug, a null character is supposed to be at the end, so why shouldn't I put it at string[100] ?

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Because that is the 101st element in the array and you have only malloc'd 100 elements

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenating strings
    By Junky in forum C Programming
    Replies: 2
    Last Post: 11-29-2009, 10:18 AM
  2. concatenating strings?
    By liri in forum C Programming
    Replies: 11
    Last Post: 08-24-2007, 05:34 PM
  3. Concatenating 2 strings
    By AngKar in forum C Programming
    Replies: 14
    Last Post: 04-25-2006, 10:51 AM
  4. concatenating more than 2 strings
    By Grunt12 in forum C Programming
    Replies: 3
    Last Post: 11-25-2005, 05:31 PM
  5. concatenating two strings
    By js_badboy in forum C Programming
    Replies: 2
    Last Post: 09-08-2003, 08:27 PM

Tags for this Thread