new_c_user
new_c_user

Reputation: 133

passing address of variable to a C function

I'm relatively new to C. I'm trying to pass an address of a variable to a function, and then have that function assign a char pointer to this variable address which was passed. The compiler doesn't complain, but the code doesn't work properly either.

typedef enum {
    VAL_1,
    VAL_2
} member_type;

char *a1="Test 123";

int func (member_type x, char *temp) {

    switch(x) {
        case VAL_1:
             temp = a1;
             return 1;
        case VAL_2:
             return 2;
    }
    return 0;
}

int main(){

    member_type b;
    static char *d1;
    b = VAL_1;
    printf("%p\n",&d1);

    func(b, &d1);

    printf("Val_1:%s\n",d1);

    return 0;
}

I get the following error when I execute it:

-bash-3.00$ ./a.out
 0x500950
 Name:(null)

Can anyone help me with how to fix it?

Upvotes: 1

Views: 3028

Answers (3)

JackCColeman
JackCColeman

Reputation: 3807

Copied your code and made only two changes in func: 1) char **temp and *temp = a1. Recall, a1 is a pointer as is *temp.

typedef enum {
    VAL_1,
    VAL_2
} member_type;

char *a1 = "Test 123";

 int func (member_type x, char **temp) {

          switch(x) {
            case VAL_1:
                *temp = a1;   // need to dereference the double ptr
                return 1;

            case VAL_2:
                return 2;
 }
 return 0;
}

int main(){

  member_type  b;
  static char   *d1;

  b = VAL_1;
  printf("%p\n", &d1);     // point to the pointer d1

  func(b, &d1);      // point to the pointer d1

  printf("Val_1:%s\n",d1);

  return 0;
}

Ran this code on Eclipse/Microsoft C compiler, and printed:

004054A0
Val_1:Test 123

Upvotes: 0

user529758
user529758

Reputation:

I find it strange that your compiler doesn't complain. I suspect that you are compiling without warnings. You should always compile using the -Wall option enabled (assuming you are using GCC or clang).

What you are doing wrong is that although you are passing the address of the char * pointer to your function, you only modify the local copy of that pointer (function arguments are passed by value in C), which has no effect outside the function. What you should do is declare the function argument as a pointer-to-pointer, and modify the original pointer by dereferencing its address:

void func(const char **p) // notice the pointer-to-pointer...
{
    *p = "bar"; // and the dereference (*) operator
}

const char *p = "foo";
printf("Before: %s\n", p);
func(&p);
printf("After: %s\n", p);

This prints:

Before: foo
Afte: bar

Upvotes: 3

Jiminion
Jiminion

Reputation: 5168

You need a double dereference:

typedef enum {
    VAL_1,
    VAL_2
} member_type;

char *a1="Test 123";

 int func (member_type x, char **temp) {

          switch(x) {
            case VAL_1:
                temp = &a1;
                return 1;

            case VAL_2:
                return 2;
 }
 return 0;
}

int main(){

  member_type b;
  static char *d1;
  b = USERNAME;
  printf("%p\n",&d1);

  func(USERNAME, &d1);

  printf("Val_1:%s\n",d1);

  return 0;
}

Upvotes: 1

Related Questions