samprat
samprat

Reputation: 2214

use of pointers in passing as an argument

I have few conceptual problem in understanding the code Below are the description

char dest[100];
char *info;

Byte *ie;

Function declaration says

getValue(Byte tag, Byte *msg, int len)

we pass something like

getValue(0X01, &info[4], 30);

here I understand that I am passing address of values to the pointer.

Now there is 2nd function

retriveValue( Byte *ie, Byte *digits, Byte totalLen)

in this function we pass something like

retriveValue(&ie[3], (Byte*)&dest, 2);  

Here , I guess, we are converting char to pointer of type byte and passing Address to it.

Now my question is:

  1. What is wrong if I convert dest to byte and pass address like (Byte)&dest?
  2. How would I pass if dest is of type char* like for eg char *dest;?

Upvotes: 2

Views: 209

Answers (2)

Philip
Philip

Reputation: 1551

"we pass something like"

getValue(0X01, &info[4], 30);

Oh do you now? Because that is scary amounts of bad karma being built up there.

First off, passing in hex values is a little weird. You could pass in a constant or a #defined value like TAG_01. In your application it may be perfectly fine, but I'd be wary.

The second argument is where it gets scary. info is a pointer (presumably set to &dest), the value that info holds is the address of the thing you're pointing to. If you wanted to pass in an address, you'd point into to the thing, and then pass in info, without the ampersand or treating it like an array. What you're doing using the square brackets [ ] to get an element of dest, and then getting address of that.

Now, it could be that you're using pointers in some ingenious way, and you really do want to reference the 4th byte after it. But I doubt it. You're probably just dinking around with pointers to see how they work, which is fine. But when you start to make stuff, if you want to pass in a message, declare a message and send that. Don't imbed a character string into part of a larger array. It's bad form. Use a struct instead.

here I understand that I am passing address of values to the pointer.

Well, you're passing an address to a function, not a pointer.


"in this function we pass something like"

retriveValue(&ie[3], (Byte*)&dest, 2);  

Again, I'm in a cold sweat over how scary wrong this is. Never do this. Who is this nefarious "we" you speak of? Are you part of some sort of horrible cult chanting an unspeakable dialect of C in an attempt to usher in a dark reign of Cthulu? Does h̡e͜͟ ͟c̀om͠e̴͝?̢̛͟

Anyway, cult aside,
What is wrong if I convert dest to byte and pass address like (Byte)&dest?

As Monsieur 32 pointed out, you're not passing an address if you do that, you're passing a Byte. Remember, that a Byte is 8 bits long. It's one char. An address is usually 32 or 64 bits, depending on the environment. So when you cast it like that, you're dropping 3/4ish of the data you need, and it doesn't fit the definition of the function. And it's a sin against nature.

How would I pass if dest is of type char* like for eg char *dest;?
Yep, just dest, all plain and vanilla by itself. This is because dest isn't a char. It's declared as dest[100], which means dest[0] is a char, but dest is the address of dest[0], like a pointer.

The array convention is really just a bit of molding over pointers to help keep things straight. There's no functional difference between *(dest+3) and dest[3]. On a technical level, arrays and pointer are different in that array declarations like dest would be treated as a const at compile time, so it wouldn't take up any RAM. A char* pointer would have it's own little hunk of RAM that you could increment while looping through your data, but only the eversleeping dark one uses pointers like that. Why is he sleeping? Endless loop because he didn't cast his pointers correctly.

Finally, if you already accepted Mr.32's answer, which was WAY ahead of mine, you really should put it back. And just say no to dead alien gods.

Upvotes: 0

Jeegar Patel
Jeegar Patel

Reputation: 27210

retriveValue(&ie[3], (Byte*)&dest, 2);  

here in 2nd argument you have taken address of dest & then pointer cast it by (Byte*)

1>What is wrong if I convert dest to byte and pass address like (Byte)&dest?

by doing this you are casting the address of dest into Byte.(not Byte pointer)

2>How would I pass if dest is of type char* like for eg char *dest;?

then no need to add "&" just write

retriveValue(&ie[3], (Byte*)dest, 2);  

Upvotes: 1

Related Questions