2D array of string

2 dimensional array

Page 1 of 1

7 Replies - 60879 Views - Last Post: 14 June 2010 - 08:00 AM Rate Topic: -----

#1 stoplookandlisten   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-January 10

2D array of string

Posted 14 June 2010 - 07:20 AM

can anyone help me with 2 dimensional array in C?
I have an exercise and the requirement is to "create 2 dimensional array that can store color and corresponding shape".

my code looks like this, but I was told that this is not 2D array. what is the correct way of creating 2d array?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>



int main(void)
{
	int i;

	char color[3][7] = {"Blue", "Green","Yellow"};
	char shape[3][9] 	= {"Square", "Circle", "Triangle"};
	for(i = 0; i < 3; i++)
	{
		printf("Color is %s  \t  Shape is	%s  \t\n", color[i], shape[i]);
		fflush(stdout);

	}

	return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: 2D array of string

#2 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: 2D array of string

Posted 14 June 2010 - 07:34 AM

Quote

create 2 dimensional array that can store color and corresponding shap

they want one 2d array to hold the information, not 2 2d arrays



You could create a 2D array of pointers, but this is why C++ is so useful. They have std::strings!! but anyway hope this helps :)
	char *color[2][3] = {{"Blue", "Green","Yellow"},
	{"Square", "Circle", "Triangle"}};

	for(int i=0; i<2; i++)
	{
		for(int j=0; j<3; j++)
			printf(color[i][j]);
		printf("\n");
	}


This post has been edited by ImaSexy: 14 June 2010 - 07:34 AM

Was This Post Helpful? 2
  • +
  • -

#3 Munawwar   User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: 2D array of string

Posted 14 June 2010 - 07:36 AM

Who told you that it is not a 2D array? I can see 2 2D char arrays.
You can put both of them into one.

EDIT:I was thinking of indexing them with multiples of two like:
char color[6][9]={"Blue", "Square", "Green", "Circle", "Yellow", "Triangle"};


EDIT: Sorry, misinterpreted the question. ImaSexy's solution is right

This post has been edited by Munawwar: 14 June 2010 - 07:44 AM

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3795
  • View blog
  • Posts: 13,738
  • Joined: 08-August 08

Re: 2D array of string

Posted 14 June 2010 - 07:39 AM

It's a 2D array of chars, but not strings.
Was This Post Helpful? 0
  • +
  • -

#5 Munawwar   User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: 2D array of string

Posted 14 June 2010 - 07:46 AM

Yup you are right. Edited my previous post.
Was This Post Helpful? 0
  • +
  • -

#6 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: 2D array of string

Posted 14 June 2010 - 07:47 AM

A better way of doing this (not standard C), is to make a structure to hold the information.

BTW, my C syntax sucks so if this is not allowed in C dont flame me :)

typedef struct
{
	char *shape, *color;
}info[3];


Was This Post Helpful? 0
  • +
  • -

#7 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: 2D array of string

Posted 14 June 2010 - 07:55 AM

Not quite. The code is not quite correct. I don't know what the [3] is doing in info[3]. If you think that's for creating an array, then you think info is the identifier for a variable, which contradicts what you are communicating with typedef.

In C, structures must be referred to as struct <somename>, so it's idiomatic to dodge this annoyance by creating an alias for the type. Using typedef.

typedef struct
{
    char *shape;
    char *color;
} ShapeInfo;
//...
ShapeInfo squareInf[10];


Was This Post Helpful? 0
  • +
  • -

#8 stoplookandlisten   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-January 10

Re: 2D array of string

Posted 14 June 2010 - 08:00 AM

thanks guys! now i get the point.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1