0

In this eg,using call by address concept in order to retrieve all the data changes which is happening in the function call...

In main(),

i)passing two arguments i)int pointer ii)pointer to constant -> which cannot change the value it is holding...

ii)Changing the values of int * and assigning some values to const void * in the function calls.

*Finally i'm trying to print the values in main()

*getting the int pointer values properly(no issues)

*getting the void pointer value as NULL..

Requirement:

Need to get the output in main() like this

main::ajskdffllagkdjdjdhdjhd(i mean to say that i need to print the void * values like this)

But i'm getting the value as main:: NULL

What shall i need to do inorder to get the expected output?

#include <stdio.h>
#include <stdlib.h>

void func(int *len,const void *pBuf);
void func2(int **len,const void **pBuf);
void func3(int ***len,const void ***pBuf);
int main()
{
        int len = 0;
        const void *pBuf;
        printf("len b4 ::%d\n",len);
        printf("%p\n",&pBuf);
        func(&len,&pBuf);
        printf("len after::%d\n",len);
        printf("%p\n",&pBuf);
        printf("main::%s\n",(const char *)pBuf);
        return 0;
}

void func(int *len,const void *pBuf)
{
        *len = 20;
        printf("func1 :: %p\n",&pBuf);
        func2(&len,&pBuf);
}

void func2(int **len,const void **pBuf)
{
        printf("func2::%p\n",&pBuf);
        **len = 30;
        func3(&len,&pBuf);
}

void func3(int ***len,const void ***pBuf)
{
        const void *pMy = "ajskdffllagkdjdjdhdjhd";
        **pBuf = pMy;
        printf("func3::%p\n",&pBuf);
        printf("func3::%s\n",(const char *)**pBuf);
        ***len = 40;
}

Output:

len b4::0
0x7fffa9c51468
func1 :: 0x7fffa9c51440
func2::0x7fffa9c51420
func3::0x7fffa9c513f0
func3::ajskdffllagkdjdjdhdjhd
len after::40
0x7fffa9c51468
main::(null)
  • Are you preparing for the International Obfuscated C Code Contest? – barak manos Sep 25 '14 at 07:19
  • Change your code to use properly typed pointers (`char*` instead of `void*`), eliminate the type casts to `const char*` and the compiler will tell you where you got it wrong. – Werner Henze Sep 25 '14 at 07:41
  • 1
    This is known as three star programming. Clearly, the solution to all problems is to add more levels of indirection. Try adding a 4th star and see if it solves the problem. – Lundin Sep 25 '14 at 07:58

2 Answers2

0

You are right with the string literal. Sorry.

However, you need to change the address where pBuf points to:

void func(int *len,const void **pBuf)
{
    *len = 20;
    printf("func1 :: %p\n",pBuf);
    func2(&len,&pBuf);
}

void func2(int **len,const void ***pBuf)
{
    printf("func2::%p\n",pBuf);
    **len = 30;
    func3(&len,&pBuf);
}

void func3(int ***len,const void ****pBuf)
{
    const void *pMy = "ajskdffllagkdjdjdhdjhd";
    ***pBuf = pMy;
    printf("func3::%p\n",&pBuf);
    printf("func3::%s\n",(const char *)***pBuf);
    ***len = 40;
}

len is an int. In order to change it, you need to call func() with the address of that int value. pBuf is a pointer. To change the address where it points to, you need to call func() with the address of that pointer.

Steffen
  • 2,381
  • 4
  • 20
  • 33
  • `"ajskdffllagkdjdjdhdjhd"` is a string literal, so it has static storage duration and a lifetime spanning the lifetime of the program. It continues to exist after the function returns. – Michael Burr Sep 25 '14 at 06:52
  • I don't think so.It will available after function returns. – Jayesh Bhoi Sep 25 '14 at 06:52
  • Tried declaring the const void * globally(same issue occuring)...Can u please explain how to put the string on the heap? – Manidhan Sep 25 '14 at 06:55
  • @Jayesh :: me too felt the same but i'm not getting the output like that....included the output along with the code.. – Manidhan Sep 25 '14 at 06:59
  • @MichaelBurr : i expected the same but its not happening...what shall i need to do inorder to make it happen – Manidhan Sep 25 '14 at 07:01
  • 2
    @Manidhan: change `**pBuf = pMy;` to `*(const void **)(**pBuf) = pMy;` or to `***(const void ****)pBuf = pMy;`. Why those work is left as an exercise for the reader. – Michael Burr Sep 25 '14 at 07:21
0

You are catching address of int with single pointer and catching the address of pointer also with single pointer.. This is problem. In functions, change *pBuf as **pBuf and **pBuf as ***pBuf and so on..

Anbu.Sankar
  • 1,326
  • 8
  • 15