HomeTOOLSEXAMPLESEXPLORE EMBEDDEDE CSERVICESECU SAMPLESRegistration
Embedded C programming Tutorial , Keil C ide , microsoftware.gr
Keil CRC and CAN BUS codes.
1. Shift led left
2.It's time for DAVE! <7/6/13>
3.Capture/Compare unit 6
4.ASC0-GPT1-MACROS
5.ASC0-FIFO-PEC
6.Analog converter
7.Memory manipulation routines
8. Recursion
9.Understanding interrupt priorities using CAPCOM2 module
10. POINTERS TO FUNCTION <4/7/13>,<4/28/13>
11.Memory models, memory types
12. The heap , part 1
13. The heap , part 2
14. The heap , part 3
15. Structure example
16. Nested structures, Array of structures.
17. Passing array of structures to function using pointers.<1/5/13>
18. Self Referential Structures
19. BITFIELDS
20. Linked list example
21. Circular linked list
22. Union example
23. Enumeration example
24. Watchdog timer example
25. Void pointer example <7/4/13>
26. The sieve of Eratosthenes
27. The stack
28. Union and bitfields as flags example. <6/23/13>
29. Look up table example. <8/11/13>
30. Seven segment display multiplexing -four digits with dot- example
31. LCD character display example - JHD162A
32. Hash table introduction example <8/27/14>
33. Array of Linked Lists example
34. Array of Linked lists-more functions included.
35. Hash table construction,searching and printing.
36. Fininte state machines- a first approach.
37. Finite state machines- two events example.
38. SPI port and an AT25128 serial eeprom hardware.
39. CRC CHECK
40. Definite Integral Calculator for Scientists, Engineers...
41 .Hamming distance of a CRC polynomial
42. Linux play starting.
43. Galois GF(2^4) Finite Field
44. Construct your own time triggered real time operating system.
45. CANBUS C CODE EXAMPLE.
17. Passing array of structures to function using pointers.<1/5/13>

This article explains by example how to pass an array of structures to a function using pointers.

The problem.

We have an array of structures   struct st sdata s[100];
Each one of these 100 structures are of st type.

and we will pass this array of structures to a function using pointers.

The strategy for the solution.

1) we make an array of pointers writing  struct st  ((* sdata apt[100] )) ;
2) Each one of these 100 pointers points  to one member of the array s[100].
    we write:
                    for(i=0;i<100;i++)


                              {
                                        apt[i]= & s[i];

                                          }

3) The function that we will call is :     void mf ( ms (  ( sdata *(*pt )[100]))   )
      with :  typedef struct st ms;
4) we pass the array of structures to the function,calling it, writing:
       
      mf (& apt);  

Important points to remember to understand the C code. 

1) pt=& apt
2)*pt=apt =& apt[0]
3)*pt+i=& apt[i]
4) *(*pt+i)= apt[i]
5)**(*pt+i)= s[i]

The examlpe C code includes for comparison both methods of catching memberes of a structure by pointer: using (*).  or ->

The C code is here                         the result


Exercise 1 : Having an array, say  struct st sdata s[100] we know that s=&s[0].
So, s+i =&s[i].
Having this in mind we have now pointers to search the elements of the array via pointers.
Rewrite the above program without using an intermediate array of pointers [steps 1). and 2). above].
You have to replace the expression   **(*pt+i). with   *(*pt+i).  and   *(*pt+i)-> with
(*pt+i)-> , the called function will be void mf( ms(*pt)[100] ) and to call this you write
mf(&s);



The C code is here                                         the result


Exercise 2 Rerwite the program of exercise 1 using a pointer pt pointing to a structure of st type. You have to replace *(*pt+i) with  * (pt+i)  and  (*pt+i) with (pt+i). The called function will be void  mf ( ms *pt  ) and to call it you write mf (s); and not  mf (&s); because s is a pointer to a structure of st  type already!

All these are  also true and about strings. Calling a string named string you have not to write &string but string simply. The name of a string is a pointer to its first character!


The C code                                                                The result

Important note
Giving the definition of a pointer we have: "Pointer is an object that is equal to an address that can be stored and changed".
Under this definition it is not exact to say that the name of an array or string or structure is a pointer.Of course they are equal with an address but this address can't be stored or changed.
We can't change the address of an array, of a string ,of a stucture.
Having a pointer p and an array s we can write p=s but we can't write s=p.
Names of arrays, stirngs and structures can't be at the left side of an assigment operator, to be lvalues.
So, always remember: An object that is equal to an address it is not sure that it is a pointer.. 


 

Home|TOOLS|EXAMPLES|EXPLORE EMBEDDEDE C|SERVICES|ECU SAMPLES|Registration