-2

My C function code keeps giving me errors, and I can't figure out whats wrong.

int * myfunc(int a)
{
    int * newNum, i;
    i = a*a;
    *newNum = i;
    return newNum;
}

2 Answers2

2

There are three types of memory or variables, automatic like in your function, static and manual. Automatic lasts while scope lasts. Static is if you declare it with static:

static int i;

It lives while program is alive. Like global variable. And finally manual where you use malloc and free to allocate and free memory. Naturally you would like to assign address of variable to pointer before return, like this:

int * newPointer = &i;

If variable is static it will keep value through function calls. Code avoids warning from compiler about returning address of local variable via assigning address of local variable to pointer so it may be good idea to run on it some kind of tool like Lint or Splint, here is discussion about such tools

Community
  • 1
  • 1
Filip Bulovic
  • 1,776
  • 15
  • 10
  • 1
    You should elaborate on how the above helps the OP. – Carcigenicate Jun 20 '15 at 19:53
  • 1
    `malloc()`&friends manage _dynamically allocated_ memory, aka the **heap**. All named variables have to be defined "manually", the compiler only handles temps "automatically". And that is not "static", but global with different linkage. – too honest for this site Jun 20 '15 at 20:57
  • @Olaf sure, problem here is that he is declaring pointer and would be assigning address of local variable to pointer, very nice way to avoid warning from compiler `function returns address of local variable`. Looking for tool which will do static analysis of code and help in such case. – Filip Bulovic Jun 20 '15 at 21:09
  • I did get the question very well. However, you should use the proper terms in your explanation. That increases confusion for beginners. Just use the terms in the standard, they are widely accepted. I do not know about _your_ compiler, but gcc _does_ warn when returning a pointer to a local. Even without warnings enabled. – too honest for this site Jun 20 '15 at 21:21
  • @Olaf still new around and not very clued up in ways how you people are trying to standardize. BTW I am also using gcc on Linux and it warns if you returning directly address but if you hide it with extra assignment it goes without warning, for that you need splint. I did double check. – Filip Bulovic Jun 20 '15 at 21:36
  • That is not _us_ standardizing, but the [C ISO standard](http://port70.net/~nsz/c/c11/n1570.html). Such answers should be well based on that. For the indirection: likely (true actually, I just verified), the compiler does not follow the chain. For such a tool, that should be easy to make it fail, too, with some more complicated approach. Never needed such a tool here. – too honest for this site Jun 20 '15 at 21:37
1

Look, newNum is a pointer to integer. So the purpose of newNum is to hold the address of any integer.

When you declared

int * newNum;

newNum is then pointing some garbage.

The following lines,

*newNum = i;

means the content of newNum will be updated by i. But did you forget, newNum holds some garbage address? Hence the value of i is assigned some garbage location.

You may try this:

/**
 * The following function will take an integer pointer from the caller. 
 * Its callers duty to check whether the pointer is initialed or not.
 */
void myfunc(int * newNum) {
    // the content of the newNum pointer points will be updated
    // as the memory address is sent here, we need not to return anything
    *newNum = (*newNum) * (*newNum);
}

int main() {
    int someInteger = 4;
    int *ptr = &someInteger;
    myfunc(ptr);
    printf("Content of the pointer: %d", *ptr);
    return 0;
}

You will get the output like,

Content of the pointer: 16

mazhar islam
  • 5,561
  • 3
  • 20
  • 41