943,969 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6620
  • C++ RSS
Oct 18th, 2006
0

C Program solution needed...

Expand Post »
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?
/////////////////
C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vijay.neo is offline Offline
2 posts
since Oct 2006
Oct 18th, 2006
0

Re: C Program solution needed...

Given your restrictions on what you can do, nothing can be done to fix your code.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 18th, 2006
0

Re: C Program solution needed...

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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 18th, 2006
0

Re: C Program solution needed...

>>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.
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 10th, 2010
0
Re: C Program solution needed...
C++ Syntax (Toggle Plain Text)
  1. #include<stdio.h>
  2.  
  3. main(){
  4.  
  5. float sales;
  6. float sal;
  7.  
  8.  
  9.  
  10. printf("Enter sales in dollars (-1 to end):");
  11. scanf("%f",&sales);
  12.  
  13.  
  14. while(sales!=-1){
  15. printf("salary");
  16. sal=sales*0.09+200;
  17.  
  18.  
  19. printf("salary is %.2f\n");
  20.  
  21. printf("\n\nEnter sales in dollars (-1 to end):");
  22. scanf("%f",&sales);
  23. }
  24. }
Last edited by WaltP; Apr 10th, 2010 at 3:31 pm. Reason: Added CODE Tags
Reputation Points: 20
Solved Threads: 0
Newbie Poster
mamdhooha is offline Offline
1 posts
since Apr 2010
Aug 27th, 2010
0
Re: C Program solution needed...
If your machine is 32-bit hardware.Then ,

If the void* points to the memory location 0x00000100h then ,
&a = 0x00000100h
&b = 0x00000104h
&c = 0x00000108h
&ch = 0x000010Ch

and when you initializing like this way , you should be ready to handle
memory exceptions otherwise this code is not robust.
Reputation Points: 86
Solved Threads: 43
Posting Pro
NicAx64 is offline Offline
532 posts
since Mar 2009
Oct 31st, 2010
-2
Re: C Program solution needed...
Using a while loop write a program that asks the user to enter a sequence of characters ending with ! and counts and outputs the number of occurrences of each of the vowels (a,o,u,i,e) only if they exist in the sequence
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ahmad chehade is offline Offline
1 posts
since Oct 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: FIRST Robot C++ Emulator/Simulator
Next Thread in C++ Forum Timeline: height of an avl tree?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC