I agree with static cases when the total number of structs inside employee is known and sizeof() returns the whole size of the array so if we want to know the number of entries we just have to divide sizeof(employee) for sizeof(struct empRec). But in dynamic cases when the user wishes to add more entries in employee array struct, if the array (employee) is bigger (1000 structs) than the number of entries (struct empRec) it has, sizeof() will not help to determine the number of entries because it will give us the size of those 1000 structs and we just want to know those we're using. Also if pointers/malloc() are used, sizeof() won't be of any help I guess.
The problem with static cases is that you are restricted in the size of the database that can be created. Declaring a 1000 element array implies that the database can't have more than 1000 records in it atleast there is no easy way to alter the number of records it can have short of modifying the array definition in the source code and re-compiling.
Quote:
Originally Posted by redoubtable
In this case, if we would want to add a few more entries dynamically we could not determine the number of existing entries (5) using sizeof(), because sizeof(employee) is the same as saying sizeof(struct empRec)*10.
the sizeof operator returns the allocated size instead of the used size for any object...so in the above case it will return the "sizeof one empRec structure times 10".
Quote:
Originally Posted by redoubtable
This is why I was advising the original poster to use a NULL/empty struct reference as the final entry so the number of entries could be calculated by counting structs until an empty one was found.
That would work only for static cases and in scenario where the last entry was NULL it implies wasting memory since only 5 out of 10 structs can be used.
Quote:
Originally Posted by redoubtable
I also agree that having 1000 entries is far too much, but I was not arguing about that. Anyway, how do you use sizeof() in dynamic cases?
Determine the size of one empRec structure and malloc'ate memory for it before adding a new record. This way the database size is not limited by the number of elements in the array.
The code snippet below can be put inside a loop to allocate space for a block of at least empRec size bytes followed by prompting the user for input and storing it in one of the elements of the empRec structure members.
I have to write an script for..
CUST: 123 trans:
some contents
CUST: 1234 trans:
some contents
Now wat i have to do is this:
CUST:123 akash trans:
some contents
CUST:1234 akash1 trans:
I have been able to add... (3 Replies)
I have to find the files older than 200 days from a path and copy them to some other directory with the current date stamp attached to it.
i have written like follows:
#!/bin/ksh
DSTAMP=$(date +"%y%m%d%H%M")
rm $CA_OUT_PATH/ftp_logs/temp
touch $CA_OUT_PATH/ftp_logs/temp
chmod 777... (1 Reply)
I have to find the files older than 200 days from a path and copy them to some other directory with the current date stamp attached to it.
i have written like follows:
#!/bin/ksh
DSTAMP=$(date +"%y%m%d%H%M")
rm $CA_OUT_PATH/ftp_logs/temp
touch $CA_OUT_PATH/ftp_logs/temp
chmod 777... (1 Reply)
I have to find the files older than 200 days from a path and copy them to some other directory with the current date stamp attached to it.
i have written like follows:
#!/bin/ksh
DSTAMP=$(date +"%y%m%d%H%M")
rm $CA_OUT_PATH/ftp_logs/temp
touch $CA_OUT_PATH/ftp_logs/temp
chmod 777... (13 Replies)
i thought about to use the commands : wc and sort and mybe more ..
i need to write a bash script that recive a list of varuables
kaka pele ronaldo beckham zidane messi rivaldo gerrard platini
i need the program to print the longest word of the list.
word in the output appears on a separate... (1 Reply)
i thought about to use the commands : wc and sort cut and mybe more ..
i need to write a bash script that recive a list of varuables
kaka pele ronaldo beckham zidane messi rivaldo gerrard platini
i need the program to print the longest word of the list.
word in the output appears on a... (0 Replies)