Thread: Problem with understanding the code.

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    45

    Problem with understanding the code.

    Code:
    #include
    Code:
    <stdio.h>
    Code:
      
    #include<stdlib.h>
    #include<string.h>
    #include<stdarg.h>
    char * konkatenacija(int n, ...);
    int main()
    {
    char *s;
    s = konkatenacija(3,
    "First", " ", "last");
    printf("%s", s);
    free(s);
    return 0;
    } char * konkatenacija(intn, ...)
    {
    char *result = 0, *next;
    inti;
    va_listargs;
    va_start(args, n);
    for (i = 0; i < n; i++)
    {
    intlen = result ? strlen(result) : 0;
    next = va_arg(args, char *);
    char *tmp = (char *)malloc(len +
    strlen(next) + 1);
    strcpy(tmp, result ? result : "");
    strcat(tmp, next);
    free(result);
    result = tmp;
    }
    va_end(args);
    return result;
    }
    What this code does it actually takes 3 strings and sends it to the function with variable number of arguments along with the number of strings used, i know that there are simple ways to do it, but i have to use function with variable arguments number, the problem is here:
    Code:
    intlen = result ? strlen(result) : 0;
     
    next = va_arg(args, char *);
    char *tmp = (char *)malloc(len +
    strlen(next) + 1);
    strcpy(tmp, result ? result : "");
    strcat(tmp, next);
    free(result);
    result = tmp;

    Why i can't use only two char* variables, one to take the next optional argument and other to contiue to like this:
    Code:
    char * konkatenacija(int n, ...)
    {
      char *result = 0, *next;
      int i;
      va_list args;
      va_start(args, n);
    
      for (i = 0; i < n; i++)
      {
        next=va_arg(args,char *); 
        strcat(result,next);
      }
      va_end(args);
      return result;
    }
    instead of dynamic allocating the memory and making temporary variable, when i try to do it with function looking like this it just does nothing, is it because i cant use strcat on the variable that has value 0 even though it is char*, or is it something else?



  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, you need to make your result point at some valid memory before you try and strcat to whatever it points to.

    In addition, you need to figure out how to "paste at text", so your code posts don't end up all mangled by whatever font gets copied along with your code.
    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.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Don't cast "malloc()" -> FAQ > Casting malloc - Cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding this code
    By sameertelkar in forum C Programming
    Replies: 11
    Last Post: 01-04-2013, 07:36 AM
  2. Need help to understanding the code
    By zbonzbon in forum C Programming
    Replies: 10
    Last Post: 02-13-2011, 10:23 PM
  3. Need some help understanding the following code
    By GTTDi in forum C Programming
    Replies: 1
    Last Post: 10-15-2010, 07:28 AM
  4. code understanding
    By elwad in forum C Programming
    Replies: 5
    Last Post: 04-18-2009, 06:57 AM
  5. help me understanding this code
    By apfelsaft in forum C Programming
    Replies: 2
    Last Post: 01-14-2004, 04:46 PM