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 01-Apr-2004, 22:09
nusstu nusstu is offline
New Member
 
Join Date: Mar 2004
Posts: 27
nusstu is on a distinguished road

help on comparing structs


Hi,

I've got a problem where I can't check a slot in an array of struct to see if it's empty. I believe the following code would be legal in JAVA but I dunno why it isn't in C.

Here's a snippet of my code. Any help appreciated.

CPP / C++ / C Code:
    int found = 0;
    struct_name newP; //some struct

    //bg is an array of struct.
    while ((!found) && (i < 20)) {
      if (bg[i] == NULL) {     //the error here is: invalid operands to binary ==
	found = 1;
	bg[i] = newP;  
	return;
      }//if
      i++;
    }//while

  #2  
Old 01-Apr-2004, 22:19
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're getting an error because bg doesn't exist in the context that you referenced it with. You must access members of a structure using dot notation, like so:

CPP / C++ / C Code:
if (newP.bg[i] == NULL)
Since the array bg is a member of the structure newP, you must access its contents by calling the name of the structured variable, followed by a dot, followed by the name of the member you want to access. If the member is also a struct, you can just keep going with dots like this.

CPP / C++ / C Code:
#include <stdio.h>

typedef struct
{
  char *first_name;
  char *last_name;
} NAME;

typedef struct
{
  NAME name;
  long id;
} STUDENT;

int main()
{
  STUDENT aaroncohn;
  STUDENT nusstu;

  aaroncohn.name.first_name = "Aaron";
  aaroncohn.name.last_name = "Cohn";
  aaroncohn.id = 123456;
  nusstu.name.first_name = "Some";
  nusstu.name.last_name = "Person";
  nusstu.name.id = 234567;

  printf("My first name: %s", aaroncohn.name.first_name);
  printf("My last name: %s", aaroncohn.name.last_name);
  printf("My ID number: %d", aaroncohn.id);
  printf("Your first name: %s", nusstu.name.first_name);
  printf("Your last name: %s", nusstu.name.last_name);
  printf("Your ID number: %d", nusstu.id);

  return 0;
}
__________________
-Aaron
  #3  
Old 01-Apr-2004, 22:47
nusstu nusstu is offline
New Member
 
Join Date: Mar 2004
Posts: 27
nusstu is on a distinguished road
Hi aaroncohn,

hm..i don't think my code was clear enough. i'll be more specific i have a struct called proc, bg is an array of proc (proc bg[20]; ). newP is declared as a proc and I put some stuff into newP. Now, I want to find an empty slot to dump newP into array bg. So there;s no such thing as newP.bg[i]

The main problem I have is how to check if a slot in a struct array is empty (NULL)?

So the code is now:
CPP / C++ / C Code:
    int found = 0;
    proc newP; //declare new proc

    //bg is an array of struct.
    while ((!found) && (i < 20)) {
      if (bg[i] == NULL) {     //the error here is: invalid operands to binary ==
         found = 1;
         bg[i] = newP; //dump newP into empty slot 
         return;
      }//if
      i++;
    }//while
Do u get the picture now ? :+
  #4  
Old 02-Apr-2004, 00:58
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 cannot perform aggregate comparisons on structured types. You would have to compare each member of the array element to NULL, then if they were all NULL, you dump newP, but aggregate comparison of a struct type to anything is not allowed. Also, for future reference, when you're referring to an array, you use the plural form of the word. IE... an array of structs. When you said "an array of struct" in your comment, I took that to mean that it was a member of the struct called newP.

Code:
Aggregate Operation Allowed on Structs? ------------------- ------------------- 1. I/O No 2. Assignment Yes 3. Arithmetic No 4. Comparison No 5. Argument Passage Yes, by value or by reference 6. Return as a function's return value Yes
__________________
-Aaron
  #5  
Old 03-Apr-2004, 03:22
nusstu nusstu is offline
New Member
 
Join Date: Mar 2004
Posts: 27
nusstu is on a distinguished road
Hi aaroncohn,

Thanks, for the tip.
 
 

Recent GIDBlogStupid Management Policies 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
Comparing Text Files (new programmer) Trust C++ Forum 2 29-Mar-2004 10:14

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

All times are GMT -6. The time now is 01:22.


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