Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

C Programming - Pointers

Status
Not open for further replies.

Jon Wilder

Active Member
I've notice with pointers, you usually declare a fixed array, then declare a pointer to use with the array. However, I'm having a difficult time understanding the difference between using a pointer and simply using an int variable as the array index.

What's the difference between say -

Code:
uint8_t array[10];
uint8_t *pointer;
pointer = array;

for(char i = 0; i < 10; ++i){
    *pointer = 0;
    ++pointer:
}

vs

Code:
uint8_t array[10];

for(char i = 0; i < 10; ++i){
    array[i] = 0;
}

When I use a pointer, I can see in the disassembly that the compiler uses the FSR/INDF register set. But other than that I don't see any other difference.
 
There is no difference... The only thing different is how we use the two..

Imagine...

char big_array[LOTS'O'ELEMENTS];

char *big_array_pointer = &big_array[0];

now big_array_pointer is exactly the same as big_array.... But you can pass the pointer easier..

You have seen this:-

char Text[] = {"Hello world"};

Then a function to print text.

void Print_My_Text_Function( char * string){
Body of function;
....;
}

Then you call function:-

Print_My_Text_Function(Text);

Same thing....
 
Wow thanks. Pointers make it super easy to pass arrays to functions as an argument. That just opened some doors.
 
Pointers also allow a lot more flexibility but also more chances to mess up "with more power comes more responsibility" as uncle Ben said.

For example a pointer could be changed to point to another array.

For example you could do this:

switch(commutation_mode)
{
case TRAPEZOIDAL:
int *commutationTable= &trapezoidalCommutationArray[0];
break;

case SINE:
int *commutationTable= &SineCommutationArray[0];
break;

(....)
}

void functionThatUsesCommutationTable(int *commutationTable)
{
//do stuff
}

Pros-
You didn't have to copy the array
No need to put a switch in every function that is going to switch modes
Cons-
You have to keep track of the size of these if they are different, but you need to do that in an Array anyway. One of the easiest ways would be to end each with a sentinel value.
 
As with every variable, struct, classe and function.. All have a memory location... that can be manipulated in exactly the same way..

One of the best usage of pointers is as Triode has just pointed out ( no pun intended )..

Fonts are used in graphical applications... Without pointers there would need to be functions to suit each font type, but pointers automate the process.. Just pass the pointer to the struct that in turn holds the pointer to the font, size colour etc...
 
Status
Not open for further replies.

Latest threads

Back
Top