Click to See Complete Forum and Search --> : C question. Declaring data on new nod to NULL, possible?


freedon
03-06-2004, 12:14 AM
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.




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 :o

nattylife
03-06-2004, 02:08 AM
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...

gameboy1234
03-07-2004, 02:39 AM
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.

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'

freedon
03-07-2004, 03:35 AM
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

freedon
03-07-2004, 10:33 AM
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!

nattylife
03-07-2004, 11:46 PM
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....