Proper way to store/output strings in a 2D array?

This is a discussion on Proper way to store/output strings in a 2D array? within the C Programming forums, part of the General Programming Boards category; I need to store strings in 2D array and output the array. I can find the strings but i'm confused ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    1

    Proper way to store/output strings in a 2D array?

    I need to store strings in 2D array and output the array. I can find the strings but i'm confused on how to store them into the array. Here is the function i have so far:

    Code:
    int store_string(char temp[10], char function[10][10])
    {
    	int j, i, n, r=0;
    	char temp[] = "hello";
    
    	function[r][10] = temp;                          /*store string into row 0 */
            r++;                                                       /*incrementing the row cause i want to store                   the next string in the next row*/
    }
    Is that correct?

    and also, how would i output the 2D array? is it like this?
    Code:
    printf("%s" , function);
    
    or
    
    printf("%s", function [10][10]);
    or neither?

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I guess what you want is:

    Code:
    char str[length][width];
    
    /* ... read str */
    
    int i;
    for(i = 0;i < length;i++)
       printf("%s\n",str[i]);
    Remember when you have a 2D array, such as str above, str[i] points to the 1D array contained at line i, which is a string.

    Regarding the assignment NO IT IS NOT CORRECT. You have to copy the contents of one string into another using strcpy(char[] destination, char[] source);

    strcpy is a function in string.h . You should look it up.

    So you would do something like :

    Code:
    #include <string.h>
    
    int store_string(char temp[10], char function[10][10])
    {
    	int j, i, n, r=0;
    	char temp[] = "hello";
    
    	strcpy(function[r], temp);                          /*store string into row 0 */
            r++;                                                       /*incrementing the row cause i want to store                   the next string in the next row*/
    }
    Last edited by claudiu; 03-10-2010 at 11:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding a character in an array of strings
    By ewoods in forum C Programming
    Replies: 3
    Last Post: 08-12-2009, 03:37 PM
  2. matching of names in 2d array of strings...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-25-2009, 09:59 AM
  3. Dictionary into a 2d Char Array... Problems.
    By Muzzaro in forum C Programming
    Replies: 10
    Last Post: 12-02-2006, 11:34 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. proper syntax for const array of strings?
    By cozman in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2001, 01:38 PM

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