Rethipher
Rethipher

Reputation: 344

pointer use in a function without address

I'm going to try to be as clear as possible, but, I do apologize if I end up not being. I've been working on this for a while now and I'm pretty new to c++.

Edit 2: cObj is a class object defined in another file

I've been writing some wrapper code in matlab, and implementation code in c++. Some of my code uses pointers as arguments to a function, whose only purpose is to be a c interface to a c++ function. The c interface function looks like below, as an example.

DLLEXPORT int GetInfo(int num, int* ChannelMask, int* ChannelIndex)
{
    return cObj->GetChannelInfo(num, ChannelMask, ChannelIndex);
}

Now here's where I have the question, and I will provide more code to illustrate.

The function GetChannelInfo also takes pointer arguments, in fact, the exact same pointer arguments as in GetInfo.

So it looks like this.

int CCan::GetChannelInfo(int num, int* ChannelMask, int* ChannelIndex)
{
    unsigned char* pChanIndex = &ChanIndex //Class variable
    unsigned long long pChanMaskTx = &ChanMaskTx; //Also a class variable

    *pChanMaskTx = Some Assignment value, lets say 5;
    *pChanIndex = Another assignment value, let's say 6; //just for arguments sake, don't care about the type.

    //Now this is passed to the original pointer value
    *ChannelMask = *pChanMaskTx;
    *ChannelIndex = *pChanIndex;
    return 0;
}

My question is this, in the first function GetInfo, why is it okay, or even legal to pass the num, ChannelMask, and ChannelIndex the way it does. i.e. return cObj->GetChannelInfo(num, ChannelMask, ChannelIndex);

Generally, when I see pointers in articles on the internet, it's something like this.

void func(int* B)
{
    *B = *B + 1;
}

int main ()
{
   int A = 5;
   func(&A);
   //Now A is equal to 6 instead of 5
}

What happened to the address being passed? In the functions I've written (which I have done based off a coworkers code), it does seem to work. But, I am beyond confused as to why, or how it works. As I understood it, a pointer needs an address at some point. But, nowhere in my code did that happen. I'm hoping somebody could shed some light on this, and what's going on. Or maybe I'm doing it completely wrong, and by sheer accident it's appears to be working.

Edit 1: num was not supposed to be a pointer. The crux of the question is in the DLLEXPORT part by the way. And GetChannelInfo is a member of a class. I instantiated cObj as a pointer object at the top of the code where GetInfo is defined

Edit 3: Why am I getting downvoted? I thought this was where you come to ask questions when you don't know/have the answer, especially when you're new. Have you all been programming so long you forgot what it's like to not know? Sheesh..

ANSWER

Edit 4: I'm going to provide a little bit more detail in case somebody else runs into this. The accepted answer explains it well. What I didn't include in my original question is the matlab code, which might be something to the effect of:

function [status] = GetChannelInfo(num)
    status = calllib('Test','GetInfo',num, 0,0);

What's happening above in the calllib function (which we don't see) is that matlab passes the address of the last two arguments in calllib to the calling function, which I would have had to do in c or c++ if I was writing the entire thing in c or c++.

Upvotes: 0

Views: 116

Answers (2)

Weather Vane
Weather Vane

Reputation: 34583

Your specific question was why it's legal to pass ChannelMask to a function which requires a pointer. As an answer, consider this

void bar(int *ptr) {
    // ... 
}

void foo(int *ptr) {
    bar(ptr);
}

int main(void) {
    int x;
    foo(&x);
}

You can see that foo and bar both require a pointer. main passes the address of x to foo, because x is not a pointer. Then foo passes ptr to bar, because ptr is already a pointer.

Upvotes: 2

Adrian Cornish
Adrian Cornish

Reputation: 23906

Ok - seems a bad example anyway

cObj looks like some global which you do not show GetChannelInfo is not a member function of a class

Function signatures do not match int GetInfo(int* num, int* ChannelMask, int* ChannelIndex) int GetChannelInfo(int num, int* ChannelMask, int* ChannelIndex)

ie num is a pointer in one but not the other

Upvotes: 1

Related Questions