Friday, May 1, 2015

C language from BaseBand Processor Eng perspective Part-8 Keywords CONST


Some of C keywords

1) const :

const * int a  ; // compilation error , this syntax is not correct.

const int * a;

Read as const to (int *)   that means the value will be const of *a , address can change , which indirectly will change what is inside (*a).


See below codes

int main()
{

int a=10;
const int*b=&a;

*b=20;
}

// This will throw error saying *b is read only memory


int main()
{

int a=10;
int c=20;
const int*b=&a;
b=&c;
}

// No error will be thrown

Also while checking I see that below code worked


void checking(int *a)
{
 *a=50;
}

int main()
{
const int b=20;
checking(&b);
printf("%d",b);
}

// Value of b is changed to 50

NOTE : ALL THESE CODES ARE CHECKED IN GCC compiler.

For the above program, if b is global variable, no compile error, but  segmentation fault at run time.


So I think we can have lot of combination, but what is important from ENG perspective is , when you are writing code their is 2 things you need to make const

1) Either variable
2) Some Pointer

For 1st we basically use
int const a   or   const int a , but if we pass this to some function , with below type

void   func_name(int *)  // then it overwrites our value, even though we might get Warnings
void func_name(const int *) // this will give compilation error

// For compilation we can set some warnings to be converted to error so that program is not build
// I will check with my colleague to see , how it can be done.


I tried one other approach also, but it seems if in passing function you pass int*, rather than const int*  ...you can change value.


The best way to write code using const is  to have int before , and 4 below combination is possible

INT CONST A  // here a value is const ; BUT as seen if we pass address value can be changed

INT CONST * A// This is also same as before, you can cannot change value of *a in local function ,                                   but can change the address it is pointing , so effectively changing the value .
                                I really don' think this one is of any use.

INT* CONST A // This is when we need a to point to const address, the value inside address can be                                   change , but not the address a points to.

But here also not able to understand below function

------------------------------------------------------------------------------------

#include

int e=90;


void checking(int  *a)
{
printf("%d %x\n",*a,a);
a=&e;
printf("%d %x\n",*a,a);
checking2(a);
}



void checking2(int  *a)
{
printf("%d %x\n",*a,a);

}



int main()
{

int b=10;
int * const a=&b;

*a=40;

printf("%d %x\n",*a, a);
checking(a);
printf("%d %x\n",*a , a);

}
----------------------------------------------------------------------------------------------------

But what I realized it , some warnings will come. We usually ignore warnings since they don't stop as from building binary.

But we should take care of them to avoid , unexpected behavior.

If in above function I defined  checking function as

void checking (int* const a)  // it will throw error , once try to change address a points to.




INT * CONST * A;

This i will leave , since I am not able to find any good real life case where we use this. I have not seen this in Modem code.



No comments:

Post a Comment