954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C Program solution needed...

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?
/////////////////

void function1(void *);
void main()
{
 int size_offset = 0;
 typedef struct 
 {
  int a;
  int b;
  int c;
  char ch;
 }A;
 A *pst = NULL;
 pst = (A*)malloc(sizeof(A)*1);
 function1((void*)pst);
}
void function1(void *pStr)
{
   // As i need to intialize the structure members. i want to access it's me
mbers.
   // How can i know the structure's 
   // memory layout in this function? 
   //Conditions:
   //1. i do not want to declare the structure as global or static.
   //2. I will pass the structure pointer as void pointer.
}
vijay.neo
Newbie Poster
2 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Given your restrictions on what you can do, nothing can be done to fix your code.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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.

void function1(void *pStr)
{
   A * ptmp = (A *) pStr;
   
   ptmp->a = 0;
   ptmp->b = 0;
   ptmp->c = 0;
   ptmp->ch = '\0';
}

if type A is in front of int main.

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 

>>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.

void function1(A *pStr)
{
  // blabla
}


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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
#include<stdio.h>

main(){

 float sales;
 float sal;



 printf("Enter sales in dollars (-1 to end):");
 scanf("%f",&sales);


 while(sales!=-1){
 printf("salary");
	sal=sales*0.09+200;


	printf("salary is %.2f\n");

	printf("\n\nEnter sales in dollars (-1 to end):");
 scanf("%f",&sales);
 }
}
mamdhooha
Newbie Poster
1 post since Apr 2010
Reputation Points: 20
Solved Threads: 0
 

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.

NicAx64
Posting Pro
536 posts since Mar 2009
Reputation Points: 86
Solved Threads: 44
 

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

ahmad chehade
Newbie Poster
1 post since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You