GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Jul-2004, 05:37
gwk gwk is offline
Awaiting Email Confirmation
 
Join Date: Jun 2004
Posts: 8
gwk is on a distinguished road

Creating N string


Hi!
I want to creat N strings where N is unknown and it is subject to recursion. How to name these string dynamically...for example I want that these string looks like
char Contents-1 = " ";
char Contents-2 = " ";
char Contents-3 = " ";
char Contents-4 = " ";
char Contents-5 = " ";
.....
.....
....
char Contents N = " ";

Later on I have to find the iteration number i.e N.
Any Help??

Thanks in Advance
Wisk
  #2  
Old 19-Jul-2004, 09:20
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
You must use an array to do this. Here's the syntax for declaring and accessing an array.

int myArray[5];

myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;
myArray[3] = 4;
myArray[4] = 5;

Character arrays are a bit different, though. All character arrays must be NULL terminated. Here's an example that also shows how to initialize an array differently.

char myArray[6] = {'A','a','r','o','n','\0'};

printf("My first name: %s\n", myArray);
printf("My first name: %c%c%c%c%c\n", myArray[0], myArray[1], myArray[2], myArray[3], myArray[4]);

As you can see, my first name is only 5 characters long, but I had to make the array 6 characters wide in order to leave room for the NULL character. An array of chars is otherwise known as a c-string, and must always be terminated by the NULL character.

The number specified in the array index brackets MUST be constant. It cannot be a variable. However, that doesn't mean that using a variable length array is impossible. The means are just a little different. I won't go into dynamic allocation right now, but you can easily look it up elsewhere on the internet.

P.S. You can also access array members through a loop. This method is very useful for intializing large arrays.

CPP / C++ / C Code:
#define array_max 21; // constant value

int myArray[array_max];

for ( int x = 0; x < array_max; x++ )
  myArray[x] = 0;

for ( x = 0; x < array_max; x++ )
  {
    if ( x % 2 )
      myArray[x] = 1;
    else
      myArray[x] = 0;
  }
In the first for loop, the entire array is initialized with zeros. This is a common activity when using arrays of any sort. In the second loop, the array is initialized conditionally. If the index number is odd, the member at that location in the array will be a 1, otherwise it will be initialized as a zero. This will produce an array that looks like this: 010101010101010101010.

The last number is a zero because the last array index is 20. Even though array_max is 21, we must remember that arrays always being with zero. If you count up from 0 to 20, you will have counted 21 members. If you attempt to access myArray[21], you will get an error, and your program will crash. The reason this happens is that you are trying to access memory that is outside the scope of the array. This will surface as an access violation before your program bites the dust, so watch out for those. They're easy to commit, especially using loops!
__________________
-Aaron
  #3  
Old 20-Jul-2004, 01:50
gwk gwk is offline
Awaiting Email Confirmation
 
Join Date: Jun 2004
Posts: 8
gwk is on a distinguished road

2 dimensional char array


It is very nice to see your in detail reply. Thank you very much for it.
Actually what I am looking for is 2 dimensional char array. OR set of char arrays where the names of each array is ending the number of the array. i.e the ith array would be the ith row if we make it through 2 dimensional array.
The point is that the ith number is not determined beforehand. It depends on the iteration and it could be any number.

I tried it with 2 dimensional array but it doesnt work.
Here is my code
Quote:
char 2Dtable[1000][1000];

//I try to copy the line contents array in 5th row of the 2Dtable which is a 2 D array itself

strcpy( 2Dtable[5][], Linecontents );

// I am not including the while loop stuff that finally stops the iteration.


When I run this code it gives the following error:
Quote:
error C2059: syntax error : ']'


Any suggestion for both options?


Quote:
Originally Posted by aaroncohn
You must use an array to do this. Here's the syntax for declaring and accessing an array.

int myArray[5];

myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;
myArray[3] = 4;
myArray[4] = 5;

Character arrays are a bit different, though. All character arrays must be NULL terminated. Here's an example that also shows how to initialize an array differently.

char myArray[6] = {'A','a','r','o','n','\0'};

printf("My first name: %s\n", myArray);
printf("My first name: %c%c%c%c%c\n", myArray[0], myArray[1], myArray[2], myArray[3], myArray[4]);

As you can see, my first name is only 5 characters long, but I had to make the array 6 characters wide in order to leave room for the NULL character. An array of chars is otherwise known as a c-string, and must always be terminated by the NULL character.

The number specified in the array index brackets MUST be constant. It cannot be a variable. However, that doesn't mean that using a variable length array is impossible. The means are just a little different. I won't go into dynamic allocation right now, but you can easily look it up elsewhere on the internet.

P.S. You can also access array members through a loop. This method is very useful for intializing large arrays.

CPP / C++ / C Code:
#define array_max 21; // constant value

int myArray[array_max];

for ( int x = 0; x < array_max; x++ )
  myArray[x] = 0;

for ( x = 0; x < array_max; x++ )
  {
    if ( x % 2 )
      myArray[x] = 1;
    else
      myArray[x] = 0;
  }
In the first for loop, the entire array is initialized with zeros. This is a common activity when using arrays of any sort. In the second loop, the array is initialized conditionally. If the index number is odd, the member at that location in the array will be a 1, otherwise it will be initialized as a zero. This will produce an array that looks like this: 010101010101010101010.

The last number is a zero because the last array index is 20. Even though array_max is 21, we must remember that arrays always being with zero. If you count up from 0 to 20, you will have counted 21 members. If you attempt to access myArray[21], you will get an error, and your program will crash. The reason this happens is that you are trying to access memory that is outside the scope of the array. This will surface as an access violation before your program bites the dust, so watch out for those. They're easy to commit, especially using loops!
  #4  
Old 20-Jul-2004, 23:27
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Try
CPP / C++ / C Code:
strcpy( 2Dtable[5], Linecontents ); 

Because strcpy() requires a pointer, only the first index should be specified.
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a .html file, check that file for links salemite C Programming Language 10 17-Jan-2008 07:56
Re: Conversion: Binary, Decimal, Hexadecimal WaltP C Programming Language 1 10-May-2006 06:49
Including Maps and strings?? maddie C++ Forum 17 05-Jul-2004 06:25
storing a token pointer as a string CoreLEx C Programming Language 1 07-Oct-2003 11:33

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:15.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.