Sponsored Content
Top Forums Programming does any one know how to solve? Post 302228838 by shamrock on Monday 25th of August 2008 03:01:21 PM
Old 08-25-2008
Quote:
Originally Posted by redoubtable
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
Code:
    static struct empRec employee[10] =
    {
        {"Peter North","4B-208",35400,{10,11,1983}},
        {"John Musa","2B-118",25400,{07,10,1993}},
        {"Paula Jeminova","1A-506",18700,{02,1,1990}},
        {"Patricia Silver","6C-123",52100,{14,21,2000}},
        {"Robert mill","4D-318",42100,{01,19,2008}}
    }

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.

Code:
pointer_to_empRec_object = malloc(sizeof(struct empRec));

while () {    /* while loop to capture user input */
    printf("get emp name ");
    fgets()   /* store emp name input above */
    printf("get emp room ");
    fgets()   /* store emp room */
    printf("get emp start date ");
    fgets()   /* store emp start date */
}   /* end of while loop */

 

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to solve this

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)
Discussion started by: akashag22
3 Replies

2. Shell Programming and Scripting

Can somebody solve this please

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)
Discussion started by: sreenusola
1 Replies

3. UNIX for Advanced & Expert Users

Can somebody solve this

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)
Discussion started by: sreenusola
1 Replies

4. UNIX for Dummies Questions & Answers

Can somebody solve this

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)
Discussion started by: sreenusola
13 Replies

5. Homework & Coursework Questions

help me to solve it thank you

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)
Discussion started by: yairpg
1 Replies

6. UNIX Desktop Questions & Answers

please help me to solve it

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)
Discussion started by: yairpg
0 Replies
All times are GMT -4. The time now is 04:25 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy