Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,121 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,758 people online right now. Registration is fast and FREE... Join Now!




Accesing a an array of an array of characters

 
Reply to this topicStart new topic

Accesing a an array of an array of characters

Pontus
9 Apr, 2007 - 10:31 AM
Post #1

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
i've made a array of an array of chars
CODE

char knoptext [][30];

now how do i get in knoptext[][1] the word "test"
User is offlineProfile CardPM
+Quote Post

BitByte
RE: Accesing A An Array Of An Array Of Characters
9 Apr, 2007 - 10:44 AM
Post #2

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 2 times
My Contributions
CODE
knoptext[][1] = "test"

User is offlineProfile CardPM
+Quote Post

Pontus
RE: Accesing A An Array Of An Array Of Characters
9 Apr, 2007 - 11:10 AM
Post #3

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
QUOTE(BitByte @ 9 Apr, 2007 - 11:44 AM) *

CODE
knoptext[][1] = "test"


then my compiler gives the error : main.cpp expected primary-expression before ']' token
User is offlineProfile CardPM
+Quote Post

BitByte
RE: Accesing A An Array Of An Array Of Characters
9 Apr, 2007 - 11:35 AM
Post #4

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 2 times
My Contributions
CODE
int rows = 5;
char *myArray[rows][30];

myArray[1][30] = "test";
myArray[2][30] = "another test";

std::cout << myArray[1][30] << std::endl;
std::cout << myArray[2][30] << std::endl;


I hope i don't get into trouble for that. myArray ( or whatever yours is called ) needs to be a pointer to each row of the array. The 30 is the length of each array.
User is offlineProfile CardPM
+Quote Post

CPlusPlusNewbie
RE: Accesing A An Array Of An Array Of Characters
9 Apr, 2007 - 10:21 PM
Post #5

New D.I.C Head
*

Joined: 9 Apr, 2007
Posts: 7


My Contributions
QUOTE(manhaeve5 @ 9 Apr, 2007 - 12:10 PM) *

QUOTE(BitByte @ 9 Apr, 2007 - 11:44 AM) *

CODE
knoptext[][1] = "test"


then my compiler gives the error : main.cpp expected primary-expression before ']' token


your error may be that you forgot the semicolon at the end of your code. sorry, im new here just reading some posts. i hope i haven't over stepped my boundaries.

User is offlineProfile CardPM
+Quote Post

Pontus
RE: Accesing A An Array Of An Array Of Characters
10 Apr, 2007 - 12:20 AM
Post #6

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
QUOTE(BitByte @ 9 Apr, 2007 - 12:35 PM) *

CODE
int rows = 5;
char *myArray[rows][30];

myArray[1][30] = "test";
myArray[2][30] = "another test";

std::cout << myArray[1][30] << std::endl;
std::cout << myArray[2][30] << std::endl;


I hope i don't get into trouble for that. myArray ( or whatever yours is called ) needs to be a pointer to each row of the array. The 30 is the length of each array.

then it only displayes the chars 't' and 'a'
User is offlineProfile CardPM
+Quote Post

CPlusPlusNewbie
RE: Accesing A An Array Of An Array Of Characters
10 Apr, 2007 - 12:47 AM
Post #7

New D.I.C Head
*

Joined: 9 Apr, 2007
Posts: 7


My Contributions
QUOTE(BitByte @ 9 Apr, 2007 - 12:35 PM) *

CODE
int rows = 5;
char *myArray[rows][30];

myArray[1][30] = "test";
myArray[2][30] = "another test";

std::cout << myArray[1][30] << std::endl;
std::cout << myArray[2][30] << std::endl;


I hope i don't get into trouble for that. myArray ( or whatever yours is called ) needs to be a pointer to each row of the array. The 30 is the length of each array.



30 is not the length of each array. it is the column location 29. if you are working with two dimensional arrays, you are working with row column format. to declare a char array with the length of 30 you would simply declare a an array like this: char myArray[30];

even better: declare a constant length such as: const int MAX_LEN = 30;
...
...
..
char myArray[MAX_LEN];

cin >> myArray; // where myArray = test;

cout << myArray; //output would be test;

This post has been edited by CPlusPlusNewbie: 10 Apr, 2007 - 01:41 AM
User is offlineProfile CardPM
+Quote Post

CPlusPlusNewbie
RE: Accesing A An Array Of An Array Of Characters
10 Apr, 2007 - 02:26 AM
Post #8

New D.I.C Head
*

Joined: 9 Apr, 2007
Posts: 7


My Contributions
oops.

This post has been edited by CPlusPlusNewbie: 10 Apr, 2007 - 02:28 AM
User is offlineProfile CardPM
+Quote Post

BitByte
RE: Accesing A An Array Of An Array Of Characters
10 Apr, 2007 - 03:00 AM
Post #9

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 2 times
My Contributions
Oop from me aswell, it did actually work though. I had -Wall and -pedantic turned off on the compiler.

CODE
#include <iostream>

int main()
{
   const int rows = 5;
   char *myArray[rows];

   myArray[0] = "test";
   myArray[1] = "another test";

   std::cout << myArray[0] << std::endl;
   std::cout << myArray[1] << std::endl;

   return 0;
}



One of the reasons the C++ string class is easier to work with. My apologies for any inconvenience.


Attached thumbnail(s)
Attached Image
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Accesing A An Array Of An Array Of Characters
10 Apr, 2007 - 08:12 PM
Post #10

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
QUOTE
I had -Wall and -pedantic turned off on the compiler.

LOL!!! I do that too!

In a multi-dimentional array in C/C++ you can think of it as a pointer of type char[Max String Length], so if I wanted to make an array of 30 byte strings:

CODE

char Array[][30] = {"zero", "one", "two", "three"};
cout << Array[0] << '\t' << Array[1] << '\t' <<Array[2] << '\t' <<Array[3];


Now if I wanted to access a particular character in one of these strings:

Array[0][1]='o'; <-- makes "zero" into "zoro"
Array[1][2]=0; <-- makes "one" into "on" (zero terminated strings)
Array[2][0]='b';Array[2][1]='o'; <-- makes "two" into "boo"
Array[3][5]='D';Array[3][6]=0; <-- makes "three" into "threeD"

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:07PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month