Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

8 errors??? Please help! 3

Status
Not open for further replies.

Rmcta

Technical User
Nov 1, 2002
478
US
I am trying to understand the use of structures with pointers. I wrote the following to learn but it does not work. Please help!


#include <stdio.h>

void my_function(int a, int b, *struct_ptr); /*what is wrong with my function declaration?*/

main()
{

struct MY_STRUCTURE{
int a;
int b;
}element;

struct MY_STRUCTURE *ptr; /*Is this the correct way to create a pointer to the structure? */


element.a = 20;
element.b = 15;

my_function(element.a, element.b, *ptr);

printf("The new values of elements a and b are now: %d and %d\n", element.a, element.b);

return 0;

}

void my_function(int a, int b, *ptr_in)
{
ptr_in ->element.a+=1; /*Is this the correct way to modify a and b in the structure ?*/
ptr_in ->element.b+=1;
}
 
This piece of code is creating a structure called MY_STRUCTURE and also declaring an instance of that structure called element.

struct MY_STRUCTURE{
int a;
int b;
}element;

Your function declaration doesn't specify the type of the variable ptr_in. It should look like this:

void my_function(int a, int b, struct MY_STRUCTURE *ptr_in)

Since the fuction is expecting a pointer, you don't want to dereference it when you call the function. Your call should look like this:

my_function(element.a, element.b, ptr);

I'm guessing your fuction wants to set the values in *ptr_in to the values passed in as "a" and "b". Since the variable "element" is not declared in the function, you can't reference them.

Your function statements should look like this:

ptr_in->a=a; /* this is equivalent to (*ptr_in).a+=1; */
ptr_in->b=b;

Does this make sense?

Dennis
 
Code:
#include <stdio.h>

/* define the structure. This needs to happen before your function prototype */

  struct MY_STRUCTURE {
    int a;
    int b;
  };


/* you don't need to pass in a and b for what you are doing */
void my_function(struct MY_STRUCTURE *struct_ptr);

int main()
{

  struct MY_STRUCTURE *ptr, element;

  element.a = 20;
  element.b = 15;

  ptr = &element; /* assign ptr the address of element */

  my_function(ptr);

  /* or alternatively, just pass the address of element */
  my_function(&element);

  printf("The new values of elements a and b are now: %d and %d\n", element.a, element.b);

  return 0;
}

void my_function(struct MY_STRUCTURE *ptr_in)
{
  ptr_in ->a+=1;
  ptr_in ->b+=1;
}
 
Thank you both very, very much. I am going to review the code line by line to learn from it.[pc1][ponder]

 
If I do the following, I get "a and b are undeclared identifiers":

void my_function(struct MY_STRUCTURE *ptr_in)
{
ptr_in->a=a;
ptr_in->b=b;
}

If I do the following: is adding 2 instead of 1 (a is 20 I shoud get 21 and not 22.)
void my_function(struct MY_STRUCTURE *ptr_in)
{
ptr_in ->a+=1;
ptr_in ->b+=1;
}

These are just example: what I wish to learn is how to perform calculations and modify elements in my structure.
 
> If I do the following: is adding 2 instead of 1 (a is 20 I shoud get 21 and not 22.)

Sorry, I should have clarified this, I showed you 2 examples of your struct showing how to assign a pointer (ptr) to point to the struct and also how to pass the address directly (&element) so the function is being called twice and the values are incremented twice:

Code:
 my_function(ptr);

  /* or alternatively, just pass the address of element */
  my_function(&element);

Simply pick the one you don't like and delete the line.

> If I do the following, I get "a and b are undeclared identifiers":

That is because a and b in my example are not declared, if I modify your code similar to your previous code, then a and b are now declared within the function:

Code:
#include <stdio.h>

/* define the structure. This needs to happen before your function prototype */

  struct MY_STRUCTURE {
    int a;
    int b;
  };


/* you don't need to pass in a and b for what you are doing */
void my_function(int a, int b, struct MY_STRUCTURE *struct_ptr);

int main()
{

  struct MY_STRUCTURE *ptr, element;

  element.a = 20;
  element.b = 15;

  ptr = &element; /* assign ptr the address of element */

  my_function(element.a,element.b,ptr);

  /* or alternatively, just pass the address of element */
  /* my_function(element.a,element.b,&element);         */

  /* another way to do it                               */
  /* my_function(ptr->a,ptr->b,&element);               */

  printf("The new values of elements a and b are now: %d and %d\n", element.a, element.b);

  return 0;
}

void my_function(int a, int b, struct MY_STRUCTURE *ptr_in)
{
  ptr_in->a = a+1;
  ptr_in->b = b+1;
}

Also, realize that in my_fuction, a and b are separate variables from ptr_in->a and ptr_in->b or element.a and element.b. I could have called them x and y or jim an joe and could pass any value in for them such as:

Code:
my_function(111,222,ptr);

I could also modify a & b within my_function without affecting ptr->a or ptr->b.

Hope that clarifies things.
 
I now understand. Thank you again for all your help.
 
Is it correct to think the following:

If I have a structure:

struct members{
int a;
int b;
};

I create a pointer to the structure:

struct members *ptr;

I can't really do anything with it, because I have not create a member to my structure?

Is that correct???
 
That is correct. You have to assign the pointer to an instance of the structure, either one already declared or one dynamically allocated using the malloc function.
 
so would this be correct?
---------------------------------

struct members{
int a;
int b;
};

struct members One, *ptr;

ptr=&One;
---------------------------------
If it is correct, then would I have to create a different pointer for each member if I wish to be able to modify data of the different members from a function?
 
I see what you're doing now. You don't need to assign the address of the structure to a pointer and then pass the pointer to the function. You can pass the address to the function directly.

So your function call would look like this:

my_function(element.a,element.b,&One);
 
Thank you. I will now practice to see how it all works.
You have been of great guidance.
Thanks!
 
To continue with the same example:

struct members{
int a;
int b;
};

main()
{

struct members One;
etc..
}


Here I wish to write a function which will modify
One.a
One.b

It is not clear to me how to write the parameters of the function and how to call the function.

Let's say that my function does not take any parameter in. It will ask the user for some info at runtime. However the function has to modify One.a, etc.

MyFunction( ? ) /*function definition*/



MyFunction( ? ) /*function call*/



 
Here's your definition:

void my_function(int a, int b, struct MY_STRUCTURE *struct_ptr);

And here's the call:

my_function({some int variable or constant},{some int variable or constant},&One);

Keep in mind that the values/variable names passed to the function are in no way related to the variable names used in the definition.
 
Can a function be declared to take in just two pointers?

int MyNewFunction(struct members *ptr_m_in, struct substance *ptr_s_in)
{
...
}

 
Absolutely! There's basically no restriction to the types or amount of parameters you can pass to a function.
 
> There's basically no restriction to the types or amount of parameters you can pass to a function.
C99 states that the maximum the compiler is allowed to enforce is at least 127 parameters.


--
 
I am trying to get this all clear in my mind now.
Here are 4 questions if anyone has time to go through them, I would really appreciate it.


struct test
{
int division;
int year;

};

struct test a, b, c;


1) If I need to pass 2 struct test type paramenters to MyFunction1,
is it correct to do it like this:

Function definition:

Myfunction1(struct test value_a, struct test value_b)
{
. . .
};


Function call: Myfunction(test.a, test.b)

===================================================


2) If I need to pass 2 struct test type paramenters to MyFunction2, and they will be modified by the function, must I create a pointer first? or one pointer for each element?

struct test a, b, c, *ptr1, *ptr2;

ptr1= &a;
ptr2=&b;

Function definition:

Myfunction1(struct test *ptr1, struct test *ptr2)
{
. . .
};


Function call: Myfunction1(ptr1, ptr2);

===================================================

3) typedef struct test{
int division;
int year;
};

test a, b, c;

I need to pass a.division to my function3. Is this correct?

Function call: MyFunction(test.a.division)
Function definition: MyFunction3(test)
{
. . .
}

===================================================

4) typedef struct test{
int division;
int year;
};

test a, b, c;

I need to pass a.division and b.division to my function4 and my function will modify both values. Do I need to create pointers:

Function definition:

Myfunction4(test)
{
. . .
}

Function call: Myfunction4( ? )
 
I'll answer each of them in a separate post to keep it readable.

Here's #1...

#include <stdio.h>

/* The struct "test" has to be declared either globally */
/* in the source or header file in order for test_function */
/* to know about it in the function declaration. */
typedef struct
{
int division;
int year;
} test ;

static void test_function (test first, test second) ;

int main ()
{
test a, b ;

a.division = 10;
a.year = 2004;
b.division = 20;
b.year = 2005;

test_function (a, b) ;

return 0;
}

static void test_function (test first, test second)
{
printf ("A.Division = %d\n", first.division);
printf ("B.Division = %d\n", second.division);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top