Thread: Array Problem

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    13

    Array Problem

    Hi,

    I am trying to make a array that can contain elements like this:

    Array[0] = "p1";
    Array[1] = "p2";
    ..
    .
    .
    .
    .
    .
    .
    Array[99] = "p100";

    how can I make such an array in C language.

    Thanks a lot.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char *arrayofpointerstocharacters[ ARRAYSIZE ];

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    At compile time (one possible way):
    Code:
    const char *text[100] =
    {
       "p1",
       /* ...the rest of them... */
       "p100"
    };
    At runtime (one possible way): declare array as a 2D char Array and use sprintf in a loop to fill it. More details on usage = better replies.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    Thanks for the replies I will try it and let you know

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    Smile

    make a 2-D char array

    eg: array[100][10];

    then declare array[0]="p1" etc

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by ramya
    make a 2-D char array

    eg: array[100][10];

    then declare array[0]="p1" etc
    That won't work, you can't assign strings like that. You'd need to strcpy it into the array:
    Code:
    strcpy(array[0], "p1");
    But if you're going to initialise them all statically, you might as well do it as has already been suggested in previous posts, with an initialisation list, or with a loop and sprintf.
    Last edited by cwr; 10-27-2005 at 12:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM