Thread: Help...accessing character arrays in a structure

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    Help...accessing character arrays in a structure

    I am having trouble accessing my character arrays in my structure. I can read the data from a file without any problem. I can copy the numbers from the main variables into the structure w/o any problems, but when I try to access character arrays from the structure after having copied data into them I run into a problem.

    Here is a sample of my code and output:

    Code:
    struct flight {
    	char[20] airline;
    	int num;
    	char[3] depart;
    	char[3] arrive;
    	char seat;
    	char meal;
    };
    struct flight flights[5];
    
    int main(){
    	int a = 0;		// counter
    	int b = 0;		// counter
    	int fileCOUNT = 0;
    	char[20] airline;	// airline name
    	int num = 0;		// airline number
    	char[3] depart;	      // depart city
    	char[3] arrive;	       // arrive city
    	char seat;		// seat preference
    	char meal;		// meal preference
    
         // open file and read in data
         // first line is a number(fileCOUNT) specifying how many lines of data need to be read in
    	  // for this case fileCOUNT = 1;
    	  // copy data from main variables into flights structure array
    		  // for airline, depart, and arrive I used same loop to copy data.
    		  // **airline, depart, and arrive = example**
    	          // (strlen(example) + 1) <- (+1) is to account for end of string character '\0'
    
    		for(a = 0; a < fileCOUNT; a++) {
    			for(b = 0; b < (strlen(example) + 1) {
    				flights[a].example[b] = example[b];
    			}
    	         // copies data from airline, depart, and arrive into respective flights[a] variables
    
    			flights[a].num = num;
    				// copies num data into flights[a].num
    		}
    
    	//format and print out headers and data from both main and flights[a] structure
    
    
    OUTPUT:
    	// exact output from my program
            // data for main and flights[a] is printed out via a loop hence the a for the index in flights structure
    
    Data from main:
    	Airline		Number	Depart	Arrive	Seat	Meal	    // depart and arrive accessed via
    	American	232	DFW	BNA	a	v		// ..."%s %s", depart, arrive...
    
    Data from flights[a]:
    	Airline		Number	Depart	Arrive	Seat	Meal	      // depart and arrive accessed via
    	American	232	DFWBNAav	BNAav	a	v   // ..."%s %s", flights[a].depart, flights[a].arrive

    As you can see I have some trouble either opying the character array data or accessing the character array data. Any help to solving this problem of mine would be much appreciated.

    Thanks in advance for any help anyone can supply to me.
    Last edited by mathewmc; 10-30-2006 at 06:07 PM.

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    How about you post one topic instead of two with the same information?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2
    my bad. first time it said post not in right link

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    21
    im also having trouble accessing structures in array but my question is much simpler than mathewmc

    example:

    Code:
    typedef struct
    {
    
    char FN[30];  /* First Name */
    char LN[30];  /* Last Name */
    int age;
    
    }Record;
    
    struct Record Precord[5];

    i want to store the entered first name by the user in variable FN in the first index of the array:

    printf("Enter first Name");
    gets(Precord[0].FN);

    is this correct?


    btw whats the difference between using typedef struct and just using struct?
    Last edited by enjoyincubus; 10-30-2006 at 06:44 PM.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by enjoyincubus
    im also having trouble accessing structures in array but my question is much simpler than mathewmc

    example:

    Code:
    typedef struct
    {
    
    char FN[30];  /* First Name */
    char LN[30];  /* Last Name */
    int age;
    
    }Record;
    
    struct Record Precord[5];

    i want to store the entered first name by the user in variable FN in the first index of the array:

    printf("Enter first Name");
    gets(Precord[0].FN);

    is this correct?


    btw whats the difference between using typedef struct and just using struct?
    Man, don't use gets, use fgets:
    Code:
    BUGS
           Never use gets().  Because it is impossible to tell without knowing the
           data in advance how many  characters  gets()  will  read,  and  because
           gets() will continue to store characters past the end of the buffer, it
           is extremely dangerous to use.  It has  been  used  to  break  computer
           security.  Use fgets() instead.
    
           It  is  not  advisable  to  mix calls to input functions from the stdio
           library with low-level calls to read() for the file descriptor  associ‐
           ated  with  the  input  stream;  the results will be undefined and very
           probably not what you want.
    A safer approach is:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct foo
    {
    &#183; char bar[10];
    }foo;
    
    
    int main()
    {
    &#183; foo dummy;
    
    &#183; fgets(dummy.bar,sizeof(dummy.bar),stdin);
    &#183; printf("%s",dummy.bar);
    
    &#183; return 0;
    
    
    }
    Last edited by Maragato; 10-30-2006 at 10:04 PM.

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by mathewmc
    As you can see I have some trouble either opying the character array data or accessing the character array data. Any help to solving this problem of mine would be much appreciated.
    When you want to store 3 chars in an array, you'll need to keep in mind that there should always be a '\0' at the end of your "string". There are, of course, times when one would not need the '\0', however, that is much more advanced than you.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    21
    Quote Originally Posted by Maragato
    Man, don't use gets, use fgets:
    Code:
    BUGS
           Never use gets().  Because it is impossible to tell without knowing the
           data in advance how many  characters  gets()  will  read,  and  because
           gets() will continue to store characters past the end of the buffer, it
           is extremely dangerous to use.  It has  been  used  to  break  computer
           security.  Use fgets() instead.
    
           It  is  not  advisable  to  mix calls to input functions from the stdio
           library with low-level calls to read() for the file descriptor  associ‐
           ated  with  the  input  stream;  the results will be undefined and very
           probably not what you want.
    A safer approach is:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct foo
    {
    · char bar[10];
    }foo;
    
    
    int main()
    {
    · foo dummy;
    
    · fgets(dummy.bar,sizeof(dummy.bar),stdin);
    · printf("%s",dummy.bar);
    
    · return 0;
    
    
    }
    i dont quite get it,

    i want to store the givin input by the user in the first index of the array?...how do i do that?

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by enjoyincubus
    i dont quite get it,

    i want to store the givin input by the user in the first index of the array?...how do i do that?
    You can't do it on my code, you would need an array of structs so you can access it's fields with "." operator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting character point into structure
    By shaileshk in forum C Programming
    Replies: 30
    Last Post: 06-02-2009, 07:19 AM
  2. Just a quick question about character arrays
    By Welshy in forum C Programming
    Replies: 3
    Last Post: 04-03-2006, 07:20 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Arrays and the POINT structure
    By incognito in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2002, 10:20 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM