Printing String Arrays

This is a discussion on Printing String Arrays within the C Programming forums, part of the General Programming Boards category; I'm trying to use strcat() to put together a string. I have the following array of strings: Code: exc[10][4]; I'm ...

  1. #1
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59

    Printing String Arrays

    I'm trying to use strcat() to put together a string. I have the following array of strings:
    Code:
    exc[10][4];
    I'm trying to concatenate the strings in this array into another string in order to make a path.
    Code:
                    
                    //build sting
                    strcat(path, "./");
                    strcat(path, asg);
                    strcat(path, "/");
                    strcat(path, exc[x]);
    The problem is that the statement in bold adds the entire array exc to the path (as in ALL the strings in array exc).

    How can I avoid this and extract individual elements from the array?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    When attempting to loop through elements of an array, I suggest you loop.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59
    Yes i've thought of concatenating char by char, but the compiler is yelling at me for doing the following:
    Code:
    strcat(path, exc[x][0]);
    The compiler yells:
    Code:
    HomeworkSetup.c:62: warning: passing arg 2 of `strcat' makes pointer from integer without a cast

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    That would add just a specific element of the exc array.
    But since the minor dimension is only [4], meaning that any proper string would be at most 3 characters, then I'd say one of these things has happened.

    1. You meant to do exc[4][10]
    2. You forgot to add \0 to your strings
    3. You forgot to count the \0 for your strings.

    Also, if path itself is too short, it may merge into exc (depending on your declaration order) and cause all sorts of additional problems.

    A complete program showing the effect would get a much more specific answer.
    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
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Had you tried something akin to...?
    Code:
       int x;
       /* ... */
       for ( x = 0; x < 10; ++x )
       {
          strcat(path, exc[x]);
       }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59
    Thank you very much Salem. I knew I should have used null terminating characters, but I didn't know that they could be used to separate strings in an array.

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Take a look to this program, i hope you understand how to use arrays of strings, dont get any worries handling array of strings is just the same as using arrays of other objects, should care though that the strings would be '\0' terminated as Salem said.

    //DevC++ IDE.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
      int strIndex = 0;
      //Declare an array of strings and set them to empty strings.
      char ArrayStrings[5][32] = {"","","","",""};
      //Strcat string to contain the final string.
      char String[512] = "C:\\Documents and Settings\\";
      srand(time(NULL));
      for(strIndex = 0; strIndex < 5; strIndex++)
      {
                   printf("Give me the string[%d]:", strIndex);
                   fgets(ArrayStrings[strIndex], sizeof(ArrayStrings[strIndex]), stdin);
                   if(ArrayStrings[strIndex][strlen(ArrayStrings[strIndex])-1] == '\n')
                       ArrayStrings[strIndex][strlen(ArrayStrings[strIndex])-1] = '\0';
      }
      //We have initiated the array of strings lets use strcat.
      strcat(String, "Kostas\\");
      //Lets choose a random string from array.
      strcat(String, ArrayStrings[rand() % 5]);
      printf("Final String:%s\n", String);
      system("PAUSE");	
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM

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