Re: [PIC] Learning C
"Forrest Christian (List Account)" <[email protected]>
| Newsgroups | gmane.comp.hardware.microcontrollers.pic |
|---|---|
| Message-ID | <CAKsZx=0GP_Q9-wHz-ALUa_Z_AVgg8y0GRvzrUH1s4hcm4boCRQ@mail.gmail.com> |
Things got a lot clearer when I finally realized that there is absolutely no difference to the compiler between: x=array[i] And: x=*(array+i) The same is true for a pointer... you can use ptr[i] interchangeably with *(ptr+i) . Once they're defined, the only difference between an array and a pointer is that you can't change the address of an array (its determined by the compiler), but the address a pointer points at can be changed at will. On Tue, Nov 19, 2024, 12:57 PM smplx <[email protected]> wrote: > > I guess the most confusing thing about "C" pointers is the way "C" treats > array names / pointers and struct names / pointers. > > e.g. > char buff[256]; > char *str; > > int x, j; > > x = buff[j]; > > x = str[j]; > > // buff above is the name of an array > // str above is a pointer to an array > > char func(char *xstr, int k) > { > return xstr[k]; > } > > x = func(buff, 23); // convert buff to a pointer and pass it > x = func(str, 23); // pass str as a pointer > > Then you see something like: > > struct FRED > { > .... > int blah; > ... > }; > > int func2(struct FRED arg) > { > return arg.blah; > } > > int func3(struct FRED *arg) > { > return arg->blah; > } > > x = func2(something); // pass struct by value > > x = func3( & something); // pass struxt by reference > > x = func(buff); // pass array by reference > > // pass by value makes copy of 'something' on stack and passes address of > copy to func2 > > // pass by reference passes address of 'something' directly to func2 > // ALSO: pass by reference passes address of 'buff' directly to func > > yes array / struct passing is inconsistant and confusing > > just remember: > > something->blah > > is equivalent to: > > (*something).blah > > > Best regards > Sergio Masci > -- > http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive > View/change your membership options at > https://mailman.mit.edu/mailman/listinfo/piclist >