C question. Declaring data on new nod to NULL, possible?

Sharky Forums


Results 1 to 6 of 6

Thread: C question. Declaring data on new nod to NULL, possible?

  1. #1
    The Medieval Mod freedon's Avatar
    Join Date
    Aug 2001
    Location
    Currently: Dallas, US. Originally: Monterrey, Mexico
    Posts
    8,669

    C question. Declaring data on new nod to NULL, possible?

    First I'll explain what I need to do. I have to use pointers with Dinamic Memory.

    This is schoolwork. The purpose is have a code of the registration of the clients on a hotel.

    (the code will be on the bottom)

    First, ask how many rooms does the hotel have. Then using a for loop, create new nods (In spanish we call it nodos, I guess NOD is the translation to english). Inside the loop, create new nod and fill it with data [the room equals to the same number as t (the variable I use to control the loop). But the other data, name, address, sex, phone number, etc I want to put NULL. At first I tried filling it with just a string of data {"----"}, didn't work, so later I declared a variable that had {"----"} and I equalled name, address, etc to that variable, didn't even compile. So I'm stuck there, I'm almost done with the rest of the code.



    Code:
    typedef struct nodo
    {
    int cuarto;            //room
    char nombre[30];         //name
    char dir[30];           //address
    char tel[8];           //phone number
    char sexo;             //sex
    struct nodo *enlace;                 //enlace is used to connect nods
    
    
    }lista;
    
    
    lista *ptr=NULL;
    lista *p1,*p2,*p3,*prin,*nuevo;
    
    int opmain, num=0,total ,t=0 ,opcuarto ,opreportecliente ,opreporte1 ,opregistro ,opmain2;
    
    char sex, vacio[4] = {"----"},vacio2[1] = {'-'};
    
    
    
    MAIN CODE
    void main(void)
    {
    The main code does work, too long to post :p 
    call function crearcuartos (create rooms)
    
    }
    
    
    
    void crearcuartos()
    {
    if(total!=0||ptr!=NULL)
    	printf("\nRooms have already been defined");
    else
    {
    	printf("\nHow many rooms in the hotel: ");
    	scanf("%d",&num);
    
    	for(t=0;t<num;t++)
    	{
    
    	nuevo=(lista*)malloc(sizeof(lista));
    
    	flushall();
    	nuevo->cuarto=(num-t);
    
    
    	flushall();
    	nuevo->nombre[t]=vacio2[t];
    	
    	flushall();
    	nuevo->dir[t]=vacio[t];
    	
    	flushall();
    	nuevo->tel=NULL;
    	
    	flushall();
    	nuevo->sexo='-';
    	
    
    	
    	if(ptr==NULL)          //if nod is the first created
    	{
    	   nuevo->enlace=NULL;  //new nod of connect is equal to NULL
    	   ptr=nuevo;         //ptr (main pointer) equals new nod position
    	}
    	else
    	{
    		nuevo->enlace=ptr;   //new nod connects to ptr
    		ptr=nuevo;           //ptr equals new position
    
    	}
    	
    
    
    }
    
    }		//**end function 'create rooms'

    As you can see in this part
    flushall();
    nuevo->nombre[t]=vacio2[t];

    flushall();
    nuevo->dir[t]=vacio[t];

    flushall();
    nuevo->tel=NULL;


    I tried 3 different ways, equal to the value of vacio2, vacio and NULL, neither work. NULL doesn't even compile, I get a "Lvalue requiered"


    flushall();
    nuevo->sexo='-';



    this one does work

    Hope someone can help me before I go crazy
    Last edited by freedonX; 03-07-2004 at 11:30 AM.
    "Est Solarus Oth Mithas"
    My Honor is My Life


    (\__/)
    (='.'=)This is Bunny. Copy and paste bunny into your
    (")_(")signature to help him gain world domination

  2. #2
    Hammerhead Shark nattylife's Avatar
    Join Date
    Jul 2003
    Location
    So Fla
    Posts
    1,303
    prolly the easiest way to do tis is to make lista a class and have the all the data (name, age, sex, etc) as private members.but this is c so that cant happen... heres something...
    instead of using the syntax:

    nuevo->nombre[t]=vacio2[t];

    use this:

    nuevo[t].nombre = vacio2[t];

    i do believe u need to access the array index BEFORE you refrenece the data member...

    and also, in the for loop just insert the code to make all data members of lista NULL there...
    eVGA 680i A1
    Intel Core 2 Quad Q6600 [email protected]
    2x2 Patriot Extreme DDR2800
    Tuniq Tower 120
    120G WD
    120G Seagate
    160G Seagate
    160G Seagate
    eVGA 8800GTX (borrowing a friends)


    http://miniprofile.xfire.com/bg/bg/type/0/badash666.png

  3. #3
    Catfish gameboy1234's Avatar
    Join Date
    Aug 2002
    Posts
    238
    Well, I see a few things right off.

    First, vacio2 is a single character array. That's what the [1] means. But you index vacio2 with t, like vacio2[t]. This makes no sense.

    What if you are to make 100 rooms? vacio2 only has 1 character in it. vacio2[99] isn't going to do anything useful at all.

    Same with vacio, and all of the other [t] subscripts. vacio only has four characters, tel eight nombre and dir 30. You can't exceed those bounds in [t] or you'll get a bad surprise, one you won't like.

    All of those [t] should be removed. Just get rid of them. (Sorry to nattylife I think he's been smoking something.)

    Second, is this C or C++? You can't copy strings with = in C. I don't think you can copy char* or char[] with = in C++ either. You have to use strcpy, for example strcpy( nuevo->nombre, vacio2). Also, ' ' doesn't make stings, it makes characters. Use " " for strings or you will break strcpy and other C string functions.

    Code:
    void crearcuartos()
    {
      if(total!=0||ptr!=NULL)
        printf("\nRooms have already been defined");
      else
      {
        printf("\nHow many rooms in the hotel: ");
        scanf("%d",&num);
        for(t=0;t<num;t++)
        {
        nuevo=(lista*)malloc(sizeof(lista));
        nuevo->cuarto=(num-t);
        nuevo->nombre[0]='\0';
        nuevo->dir[0]='\0';
        nuevo->tel[0]='\0';
        nuevo->sexo='-';
        if(ptr==NULL) //if nod is the first created
        {
          nuevo->enlace=NULL; //new nod of connect is equal to NULL
          ptr=nuevo; //ptr (main pointer) equals new nod position
        }
        else
        {
          nuevo->enlace=ptr; //new nod connects to ptr
          ptr=nuevo; //ptr equals new position
        }
      }
    } //**end function 'create rooms'

  4. #4
    The Medieval Mod freedon's Avatar
    Join Date
    Aug 2001
    Location
    Currently: Dallas, US. Originally: Monterrey, Mexico
    Posts
    8,669
    Originally posted by nattylife


    and also, in the for loop just insert the code to make all data members of lista NULL there...
    I also tried that, but thanks for the suggestion

    Originally posted by gameboy1234


    Second, is this C or C++? You can't copy strings with = in C. I don't think you can copy char* or char[] with = in C++ either. You have to use strcpy, for example strcpy( nuevo->nombre, vacio2). Also, ' ' doesn't make stings, it makes characters. Use " " for strings or you will break strcpy and other C string functions.
    I don't have the code here with me, but strcpy sounds just the thing I need! I have to use strcmp on other functions, how could I forget strcpy. BTW this is plain C.

    I'll post in the morning if it worked or not
    "Est Solarus Oth Mithas"
    My Honor is My Life


    (\__/)
    (='.'=)This is Bunny. Copy and paste bunny into your
    (")_(")signature to help him gain world domination

  5. #5
    The Medieval Mod freedon's Avatar
    Join Date
    Aug 2001
    Location
    Currently: Dallas, US. Originally: Monterrey, Mexico
    Posts
    8,669
    YES THAT WAS IT!!!

    I just had to change the variable
    vacio[4] = {"----"}, to vacio[] = "----"
    I was getting lots of garbage

    thank you thank you!
    "Est Solarus Oth Mithas"
    My Honor is My Life


    (\__/)
    (='.'=)This is Bunny. Copy and paste bunny into your
    (")_(")signature to help him gain world domination

  6. #6
    Hammerhead Shark nattylife's Avatar
    Join Date
    Jul 2003
    Location
    So Fla
    Posts
    1,303
    haha yer right, didnt even think about the char array thing goin on there... thats funny too cuz i had that prob last week, im working on a circle list using array of strings... forgot thatif i do
    string Player[some number];
    that it makes an array of that number's size... so i just did an array of poi9nters to strings... lots easier... too bad i dont smoke nuthin... i should start now....
    eVGA 680i A1
    Intel Core 2 Quad Q6600 [email protected]
    2x2 Patriot Extreme DDR2800
    Tuniq Tower 120
    120G WD
    120G Seagate
    160G Seagate
    160G Seagate
    eVGA 8800GTX (borrowing a friends)


    http://miniprofile.xfire.com/bg/bg/type/0/badash666.png

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •