Arrays of strings >_<

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chickendude
    Away from Computer
    FFR Simfile Author
    • Sep 2003
    • 1901

    #1

    Arrays of strings >_<

    Yeah,
    I'm programming in C here, but anyone can help.

    I don't understand how to work with an array of strings (2D array of chars).
    I'm trying to send them into functions and use them, but when I compile and run the program I get this HUGE HUGE HUGE pile of crap that scrolls the window down like 25 pages.

    The program itself is really simple, just a merge sort. I just don't know how you send an array of strings into a function as a parameter.

    Neither, strings** or strings[][] are working. Strings** doesn't seem like it makes sense, but strings[][] seems like it should work, but it doesn't.

    Someone help <_<.
  • Hilary_Clinton
    FFR Player
    • Feb 2006
    • 33

    #2
    RE: Arrays of strings &gt;_&lt;

    i'm guessing you'd have to call them based on their position within the array... like x[j], where x is your array of strings and j is your loop variable working with the positions in the array...

    closest thing i have is an array of integers program that i wrote last semester. It may help with the syntax of working with the array. Beyond that, I'm not completely sure what you're looking to do, so its hard to help. Also, my programming is in Java. Similar to C but not exactly the same...

    Anyway, here is a program... ignore it if its completely useless:

    Code:
    public class Asgn7
    /*
     *
     * Generates n random numbers.  Store them in an array.  Print the numbers in the array backwards.
     *
     */
     
     &#123;
     	public static void generate&#40;int n&#41;
     	&#123;
     		int&#91;&#93; x = new int&#91;n&#93;;
     		int rand;
     		int index = 0;
     		int len = x.length;
     		for&#40;int j = 1; j <= n; j++&#41;
     		&#123;
     			rand = &#40;int&#41;&#40;10*Math.random&#40;&#41; &#41;;
     			x&#91;index&#93; = rand;
     			index++;
     		&#125;
     		for&#40;int k = len-1; k >= 0; k--&#41;
     		&#123;
     			System.out.println&#40;x&#91;k&#93; &#41;;
     		&#125;
     	&#125;
     	 		
     	public static void main&#40;String&#91;&#93; boob&#41;
     	&#123;
     		final int z = 7;
     		generate&#40;z&#41;;
     	&#125;
     &#125;
    I was the REAL 42nd President.

    Comment

    • Tokzic
      FFR Player
      • May 2005
      • 6878

      #3
      RE: Arrays of strings &gt;_&lt;

      Hilary Clinton knows her haxx.

      I'd help, but I'm afraid I do not know anything exquisite outside of Pascal.

      Last edited by Tokzic: Today at 11:59 PM. Reason: wait what

      Comment

      • stlunatic0124
        FFR Player
        • Feb 2005
        • 3228

        #4
        RE: Arrays of strings &gt;_&lt;

        chickendude.. arrays of strings..

        char arrayname[NUMBER_OF_CELLS][MAX_LENGTH_OF_STRING];

        I got an A in programming for electrical engineers last semester, which was in C.. from what I remember (even though it all left my head immediately after the final), I'm pretty sure that is correct.

        Comment

        • chickendude
          Away from Computer
          FFR Simfile Author
          • Sep 2003
          • 1901

          #5
          RE: Arrays of strings &gt;_&lt;

          thanks, that really helped

          in my program, I accidentally went out of the boundaries of my array of strings... so when I printed it I got crap data

          except.... I got data that looked like this
          "Family15Model4St(infinitysign)"

          o_O
          yeahg, so my program is on the verge of working. I basically, had to put in the parameters the size when sending into a function. I also had to make the first character of the next string null so I'd know where my array ended.

          Comment

          • stlunatic0124
            FFR Player
            • Feb 2005
            • 3228

            #6
            RE: Arrays of strings &gt;_&lt;

            I win!

            Comment

            • alainbryden
              Seen your member
              FFR Simfile Author
              • Dec 2003
              • 2873

              #7
              Re: Arrays of strings &gt;_&lt;

              Arrays of strings are very strange. You have to be extremely careful how you define them

              char array[size][string_length] is not an array of strings, it is a two dimentional array of characters, and has different properties it works in some cases. If you are going to make it, you need to make sure that every 'string' you put in there ends in '/0' or functions like strcpy and strlen will fail and create errors in the array.

              char* array[size] is the proper way to define an array of strings, and you need to program differently with it. I recently went through some very difficult times coming to an extensive understanding of how to deal with arrays of strings in C99. If you need some help though, and no one else seems to have expertise in C, I'm certainly the man to ask. Try to catch me on AIM.

              The C and Java (minus GUI) expert,

              Alain
              Last edited by alainbryden; 02-25-2006, 02:54 AM.
              ~NEIGH

              Comment

              Working...