Declaring array ..

This is a discussion on Declaring array .. within the C Programming forums, part of the General Programming Boards category; I would like to know if it is possible to store one string and one integer in a array ?? ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    41

    Declaring array ..

    I would like to know if it is possible to store one string and one integer in a array ??

    like i would like to store

    "john" "45"
    "darwin" "56"
    "felicious" "20"

    in an array. Is that possible ???

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    char *array[] = {
      "john", "45",
      "darwin", "56",
      "felicious", "20"
    };
    Of course, a better way to maintain grouping would be to use a structure:
    Code:
    struct s {
      char string[SIZE];
      int integer;
    };
    
    struct s array[] = {
      { "john", 45 },
      { "darwin", 56 },
      { "felicious", 20 }
    };
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    41
    If i use the first option, when i wanted to print that time, woudlnt it be ..?

    printf("%s", array[0]); // john
    printf("%s", array[1]); // 45
    printf("%s", array[2]); // darwin
    printf("%s", array[3]); // 56
    printf("%s", array[4]); // felicous
    printf("%s", array[5]); // 20

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >when i wanted to print that time, woudlnt it be ..?
    Try it and see.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    41
    May i know whats wrong with this code ?? There isn't any error while compiling. But when i try to build the file that time, it display the following error


    "Linking...
    prac3a.obj : error LNK2001: unresolved external symbol _punctuationx
    Debug/prac3a.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe."

    Code:
    struct s {
      char string[10];
      int integer;
    };
    
    
    struct s reservedx[];
    
    void hello()
    {
    	for(i=0; i <= 8; i++)
    	{
       reservedx[i].integer=0;
       strcpy(reservedx[i].string,"hello");
    	}
    }

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    41
    i know whats wrong already. I didnt declare the size of the array.
    Thanks anyway..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 11-06-2005, 08:29 PM
  2. Error with an array
    By SWAT_LtLen in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2005, 10:00 AM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. help with declaring array in which the user specifies size
    By ibanezrgking in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2002, 09:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21