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

Recommended Answers

All 6 Replies

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.

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

#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);
 }
}
commented: Thanks, but no thanks -1
commented: Bad code, poorly posted, no explanation. Worthless. +11

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.