C Program solution needed...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2006
Posts: 2
Reputation: vijay.neo is an unknown quantity at this point 
Solved Threads: 0
vijay.neo vijay.neo is offline Offline
Newbie Poster

C Program solution needed...

 
0
  #1
Oct 18th, 2006
Hi friends,

As i a writting small C program to pass the structure pointer in the function. i am endup with small problem, plz let me know how to crack this one?
/////////////////
  1. void function1(void *);
  2. void main()
  3. {
  4. int size_offset = 0;
  5. typedef struct
  6. {
  7. int a;
  8. int b;
  9. int c;
  10. char ch;
  11. }A;
  12. A *pst = NULL;
  13. pst = (A*)malloc(sizeof(A)*1);
  14. function1((void*)pst);
  15. }
  16. void function1(void *pStr)
  17. {
  18. // As i need to intialize the structure members. i want to access it's me
  19. mbers.
  20. // How can i know the structure's
  21. // memory layout in this function?
  22. //Conditions:
  23. //1. i do not want to declare the structure as global or static.
  24. //2. I will pass the structure pointer as void pointer.
  25. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: C Program solution needed...

 
0
  #2
Oct 18th, 2006
Given your restrictions on what you can do, nothing can be done to fix your code.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: C Program solution needed...

 
0
  #3
Oct 18th, 2006
Your type A, can it be outside of the main function? Still your pst struct is local beside that.
NOTE: Instead of void main use int main.
  1. void function1(void *pStr)
  2. {
  3. A * ptmp = (A *) pStr;
  4.  
  5. ptmp->a = 0;
  6. ptmp->b = 0;
  7. ptmp->c = 0;
  8. ptmp->ch = '\0';
  9. }
if type A is in front of int main.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,616
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: C Program solution needed...

 
0
  #4
Oct 18th, 2006
>>pst = (A*)malloc(sizeof(A)*1);

1. The "*1" is unnecessary and spurious.
2. C programs do not require typecasting. So remove that typecast. If your compiler complains then your are apparently writing a c++ program.

3. Move that structure above the functuion main() so that it is visible to all other functions. Then change function1 parameter to use that structure instead of passing a void pointer. You will need to change the function prototype at the top of your program too.
  1. void function1(A *pStr)
  2. {
  3. // blabla
  4. }

4. Come up with a better naming convention than "A". That is a sure sign of poor programming habits. You are new at this, so learn the right way at the very start.
Last edited by Ancient Dragon; Oct 18th, 2006 at 10:30 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 1
Reputation: 18jainabhishek is an unknown quantity at this point 
Solved Threads: 0
18jainabhishek 18jainabhishek is offline Offline
Newbie Poster

Re: C Program solution needed...

 
0
  #5
Mar 31st, 2009
plz solve this problem


Consider the ADT set that represents a collection of integers. The ADT should support standard set operations:

S = init();
/* Initialize S to the empty set */

isEmpty(S);
/* Return true if and only if S is the empty set */

isSingleton(S);
/* Return true if and only if S contains only one element */

isMember(S,a);
/* Return true if and only if a is a member of the set S */

S = addElement(S,a);
/* Add the element a to the set S. If a is already in S,
there will be no change, else a new element is to be inserted. */

S = delElement(S,a);
/* Remove the element a from the set S. No change if a is not
a member of S. */

S = union(U,V);
/* Assign to S the union of the sets U and V */

S = intersection(U,V);
/* Assign to S the intersection of the sets U and V */

S = difference(U,V);
/* Assign to S the set difference U - V */

S = symmDiff(U,V);
/* Assign to S the symmetric difference (U - V) union (V - U) */

printElements(S);
/* Print the elements of the set S */

Implement the set ADT using dynamic arrays. You will need to use realloc to change the size of an array that was dynamically allocated earlier.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: C Program solution needed...

 
0
  #6
Mar 31st, 2009
Originally Posted by 18jainabhishek View Post
plz solve this problem
A - no one is likely to solve your problem for you. You must attempt the solution, show your work, and we'll help you get to the solution you need.

B - don't ask a question by replying to an unrelated thread - that's hijacking and not appreciated

B+ - really don't do this with a thread that's over 2 years old!

Please read the sticky posts at the top of this forum to learn the rules that will make your visit here more pleasant and productive.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC