开发者

Local synonymous variable to non exact type

开发者 https://www.devze.com 2023-03-11 13:09 出处:网络
I\'m a little bit new to C so I\'m not familiar with how I would approach a solution to this issue. As you read on, you will notice its not critical that I find a solution, but it sure would be nice f

I'm a little bit new to C so I'm not familiar with how I would approach a solution to this issue. As you read on, you will notice its not critical that I find a solution, but it sure would be nice for this application and future reference. :)

I have a parameter int hello and I wan't to make a synonomous copy of not it.

f(int hello, structType* otherParam){  
  // I would like to have a synonom for (!hello)
}

My first thought was to make a local constant, but I'm not sure if there will be additional memory consumption. I'm building with GCC and I really don't know if it would recognize a constant of a parameter (before any modifications) as just a synonymous variable. I don't think so because the parameter could (even though it wont be) changed later on in that function, which would not effect the constant.

I then thought about making a local typedef, but I'm not sure exactly the syntax for doing so. I attempted the following:

typedef (!hello) hi;

However I get the foll开发者_JAVA技巧owing error.

D:/src-dir/file.c: In function 'f':
D:/src-dir/file.c: 00: error: expected identifier or '(' before '!' token

Any help is appreciated.


In general, in C, you want to write the code that most clearly expresses your intentions, and allow the optimiser to figure out the most efficient way to implement that.

In your example of a frequently-reused calculation, storing the result in a const-qualified variable is the most appropriate way to do this - something like the following:

void f(int hello)
{  
    const int non_hello = !hello;

    /* code that uses non_hello frequently */
}

or more likely:

void x(structType *otherParam)
{  
    char * const d_name = otherParam->b->c->d->name;

    /* code that uses d_name frequently */}
}

Note that such a const variable does not necessarily have to be allocated any memory (unless you take its address with & somewhere) - the optimiser might simply place it in a register (and bear in mind that even if it does get allocated memory, it will likely be stack memory).


Typedef defines an alias for a type, it's not what you want. So..

  • Just use !hello where you need it

Why would you need a "synonym" for a !hello ? Any programmer would instantly recognize !hello instead of looking for your clever trick for defining a "synonym".


Given:

f(int hello, structType* otherParam){  
  // I would like to have a synonom for (!hello)
}

The obvious, direct answer to what you have here would be:

f(int hello, structType *otherParam) { 
    int hi = !hello;
    // ...
}

I would not expect to see any major (or probably even minor) effect on execution speed from this. Realistically, there probably isn't a lot of room for improvement in the execution speed.

There are certainly times something like this can make the code more readable. Also note, however, that when/if you modify the value of hello, the value of hi will not be modified to match (unless you add code to update it). It's rarely an issue, but something to remain aware of nonetheless.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号