Concatenating strings (dynamic array using pointers)

This is a discussion on Concatenating strings (dynamic array using pointers) within the C Programming forums, part of the General Programming Boards category; Hey all, Any ideas on how to concatenate strings that are refered to by an array of pointers to pointers? ...

  1. #1
    Goscinny or Uderzo?
    Join Date
    Jun 2004
    Posts
    33

    Concatenating strings (dynamic array using pointers)

    Hey all,

    Any ideas on how to concatenate strings that are refered to by an array of pointers to pointers? This is what I have at the moment:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_NO_STR 10
    #define MAX_LGT_STR 99
    
    
    main(int argc, char *argv[]) {
       char  **strings;
       int elements, i;
    
       strings = malloc( MAX_NO_STR * sizeof(char *) );
       if(strings == NULL) {
          fprintf(stderr, "out of memory\n");
          exit(1);
       }
       for(i = 0; i < MAX_NO_STR; i++) {
          strings[i] = malloc(MAX_LGT_STR * sizeof(int));
          if(strings[i] == NULL) {
             fprintf(stderr, "out of memory\n");
             exit(1);
           }
       }
    
       if(argc != 3) {
          strings[0]="Incorrect number of arguments for message ";
          strings[1]= "argv[2] will be used here";
          strings[2]=" generation";
          elements=3;
          catstrings(elements, strings);
          exit(1);
       } 
    
       for(i=0; i<MAX_NO_STR; i++) free(strings[i]);
       free(strings);
    
       exit(0);
    }
    
    catstrings(int elements, char **logmsg) {
       int i;
    
       for(i=1; i<elements; i++) {
          strcat(logmsg[0], logmsg[i]);
       }
    
    }
    The code compiles ok, but I get a segmentation fault each time I try to run it. I've tracked the error down to the line in the catstrings function that is supposed to perform the concatenation. Any ideas how I could achieve this or what the problem is?

    Thanks in advance for your help.

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Code:
    strings[0]="Incorrect number of arguments for message ";
    Strings[0] is pointing to allocated space and you are changing the pointer to a const string. Try strcpy instead:
    Code:
    strcpy(strings[0], "Incorrect number of arguments for message ");

  3. #3
    Goscinny or Uderzo?
    Join Date
    Jun 2004
    Posts
    33
    Thanks Monster! That's done the trick!

    The seperate strings were printing ok before concatenation so it didn't occur to me! That's what I get for looking at the same piece of code for too long!

    Thanks again.


  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    You're welcome.

    b.t.w. you should also do this (strcpy) for strings[1] and strings[2], otherwise you will get problems releasing the allocated memory

  5. #5
    Goscinny or Uderzo?
    Join Date
    Jun 2004
    Posts
    33
    Cheers. I figured as much and I've the whole thing working now for much larger systems as was the original plan.

    Thanks.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're also going to have a nasty problem here, when you've concatenated all of these strings, and you realize that you've just run out of space on the first string you're tacking them all onto. You never check the size of any of your strings, and are assuming that all combine, they aren't going to run over 99 characters.

    In your test program, this means that none of your "strings" can be more than 10 characters, because otherwise, your last concatenation is going to be a BadThingTM.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    > strings[i] = malloc(MAX_LGT_STR * sizeof(int));
    They're chars, not ints
    strings[i] = malloc(MAX_LGT_STR * sizeof(char));

    Or much easier to remember, and much easier to maintain
    strings[i] = malloc(MAX_LGT_STR * sizeof *strings[i] );
    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.

  8. #8
    Goscinny or Uderzo?
    Join Date
    Jun 2004
    Posts
    33
    Quote Originally Posted by Salem
    > strings[i] = malloc(MAX_LGT_STR * sizeof(int));
    They're chars, not ints
    strings[i] = malloc(MAX_LGT_STR * sizeof(char));

    Or much easier to remember, and much easier to maintain
    strings[i] = malloc(MAX_LGT_STR * sizeof *strings[i] );
    Thanks for pointing that one out to me. I was adapting part of the code from a previous program and hadn't noticed that I didn't update that part.

    Cheers.

  9. #9
    Goscinny or Uderzo?
    Join Date
    Jun 2004
    Posts
    33
    Quote Originally Posted by quzah
    You're also going to have a nasty problem here, when you've concatenated all of these strings, and you realize that you've just run out of space on the first string you're tacking them all onto. You never check the size of any of your strings, and are assuming that all combine, they aren't going to run over 99 characters.

    In your test program, this means that none of your "strings" can be more than 10 characters, because otherwise, your last concatenation is going to be a BadThingTM.

    Quzah.
    Thanks for the tip. I'm in the middle of writing something to do that as we speak

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  3. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  4. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 04:03 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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