In the book "Understanding pointers in C" there are some solved problems after an argument has been explained. Page 22, problem N.5I am going to attach the code and the explanation. After that, there will be my question.

Explanation The reason for the error is simple. The integers being passed to check( ) are collected in i and j, and then their addresses are assigned to p and q. Then in the next statement the value of i is tested against 45, and either the address stored in p or the address stored in q is returned. It appears that this address would be collected in c in main( ), and then would he printed out. And there lies the error. The function check( ) is not capable of returning an integer pointer. All that it can return is an ordinary integer. Thus just declaring c as an integer pointer is not sufficient. We must make the following modifications in the program to make it work properly


Pointer In C Yashwant Kanetkar Free Download


tag_hash_104 🔥 https://urloso.com/2yjYWK 🔥



In my opinion this does not make sense.The author, to make an error on purpose on the first piece of code, tries to use in the main, a pointer that points to a memory area that is not allocated.But the way he "tries" to fix the problem is not correct at all, he did not change anything! Instead he should have malloc'd some memory areas in check() function. Am I correct?

In your first example, when the compiler sees the line c = check(10, 20); it doesn't yet know what this function will return. The standard says the compiler should then assume that the return is an int. And this is what this is all about: the size of an int may be different than the size of a int * (a pointer).

To make the compiler always emit the correct machine code for this, it must know that check returns a pointer which is why it needs to be declared. That's what the second example does: it tells the compiler "there will be a function named 'check' and it will look like this".

Apart from that, the example is really bad. The arguments to check are pushed on the stack and then the function returns pointers to these stack locations. But it's not guaranteed that these stack locations are valid any more after the function exits. This is undefined behaviour which means the compiler is allowed to do anything it likes, even make your computer explode.

This specific example should actually work as it's only printing the pointer (that is, the address it is pointing to), but it is not actually dereferencing it (this is, reading from the address the pointer points to). On most machines/compilers even dereferencing should work "correctly", but you may not rely on it.

However the code is flawed beyond that, because the check function returns a pointer to a local variable (the parameter i or j, depending on i's value), which is an undefined behavior (thankfully, that pointer is never dereferenced).

This scenario is called a dangling pointer. When the Check() execution is completed, the memory allocated for the local variable may be allocated to some other function, so accessing that memory leads to undefined results.

It needs a forward declaration. By default, the return type will be considered as int, but here int* is used, so adding int * check(int, int); will solve Error message: Non portable pointer assignment in main.

You are returning addresses of local variables. Dereferencing those is undefined behaviour in C. Both p and q are pointers to the local variables of the function check(). After the function check() returns control to main(), then p and q point to undefined values. Further, do make this following correction to your code:

Proficiency in using pointers is essential for optimizing memory management, developing high-performance algorithms, and gaining precise control over your C/C++ code. This course is your path from novice to ninja in mastering the art of C/C++ pointers. 0852c4b9a8

free download of microsoft security essentials 2012

ebuddy xms free download java

free download mp3 indo