![]() |
|
#1
|
|||
|
|||
Creating N stringHi!
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
|
||||
|
||||
|
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:
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
|
|||
|
|||
2 dimensional char arrayIt 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:
When I run this code it gives the following error: Quote:
Any suggestion for both options? Quote:
|
|
#4
|
||||
|
||||
|
Try
CPP / C++ / C Code:
Because strcpy() requires a pointer, only the first index should be specified. __________________
Age is unimportant -- except in cheese |
Recent GIDBlog
Observations of Iraq by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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