0

I have created this very simple program. My goal is to have the output say String: hello world James but I want the hello world to be malloced in my test_function. Can someone explain to me how I can make my_intro = "hello world" and name my_name = "James". This problem is revolves around how I can parse the malloc-ed char value back to my main function. This is not a duplicate of changing ints. This is parsing char *

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

void test_function(char *a_intro, char *a_name);

int main(void) {
    char *my_intro;
    char *my_name;
    test_function(my_intro, my_name);

    printf("String: %s %s\n", my_intro, my_name);
}

void test_function(char *a_intro, char *a_name) {

    char *intro = malloc(20);
    char *name = malloc(20);
    strcpy(intro, "hello world");
    strcpy(name, "James");

    a_intro = intro;
    a_name = name;

}

But the error I get is:

testcode.c: In function 'main':
testcode.c:10:5: error: 'my_intro' is used uninitialized in this function [-Werr
or=uninitialized]
     test_function(my_intro, my_name);
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
testcode.c:10:5: error: 'my_name' is used uninitialized in this function [-Werro
r=uninitialized]
cc1.exe: all warnings being treated as errors

Another possible solution which doesn't work:

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

void test_function(char *a_intro, char *a_name);

int main(void) {
    char *my_intro;
    char *my_name;
    test_function(&my_intro, &my_name);

    printf("String: %s %s\n", my_intro, my_name);
}

void test_function(char *a_intro, char *a_name) {

    char *intro = malloc(20);
    char *name = malloc(20);
    strcpy(intro, "hello world");
    strcpy(name, "James");

    *a_intro = intro;
    *a_name = name;

}

Error:

testcode.c: In function 'main':
testcode.c:10:19: error: passing argument 1 of 'test_function' from incompatible
 pointer type [-Werror=incompatible-pointer-types]
     test_function(&my_intro, &my_name);
                   ^
testcode.c:5:6: note: expected 'char *' but argument is of type 'char **'
 void test_function(char *a_intro, char *a_name);
      ^~~~~~~~~~~~~
testcode.c:10:30: error: passing argument 2 of 'test_function' from incompatible
 pointer type [-Werror=incompatible-pointer-types]
     test_function(&my_intro, &my_name);
                              ^
testcode.c:5:6: note: expected 'char *' but argument is of type 'char **'
 void test_function(char *a_intro, char *a_name);
      ^~~~~~~~~~~~~
testcode.c: In function 'test_function':
testcode.c:22:14: error: assignment makes integer from pointer without a cast [-
Werror=int-conversion]
     *a_intro = intro;
              ^
testcode.c:23:13: error: assignment makes integer from pointer without a cast [-
Werror=int-conversion]
     *a_name = name;
             ^
cc1.exe: all warnings being treated as errors
melpomene
  • 84,125
  • 8
  • 85
  • 148
Kiwa
  • 215
  • 2
  • 10
  • What is unclear about the errors? `test_function` takes 2 arguments, you call it with 3. There is no variable `my_str` in main, you only have `my_intro` and `my_name`. – mch Nov 15 '18 at 09:06
  • 1
    In C all function arguments are passed *by value*. That means they are *copied*. The arguments inside a function are distinct and separate variables from what was used when calling the function. Modifying the copy inside the function will *not* modify the original variable. To solve that (and your error) please do some research about *emulating pass by reference in C*. – Some programmer dude Nov 15 '18 at 09:07
  • @mch its not. I tried the solution in that answer already. That answer is about ints. I tried what solution for my char. It didn't work – Kiwa Nov 15 '18 at 09:12
  • @Kiwa The fundamentals of pass-by-value don't change depending on the type of your variables. – melpomene Nov 15 '18 at 09:16
  • @melpomene I've edited my question with the logic of the duplicate. – Kiwa Nov 15 '18 at 09:17
  • If you want to change a variable in a function, you have to pass the address of the variable. Change `void test_function(char *a_intro, char *a_name)` in your last solution to `void test_function(char **a_intro, char **a_name)` – mch Nov 15 '18 at 09:19
  • @mch is there a better way then parsing a double pointer? – Kiwa Nov 15 '18 at 09:20
  • @Kiwa The duplicate says to change your function to take a pointer to your variable instead. Your variable has type `char *`, so a pointer to that has type `char **`. – melpomene Nov 15 '18 at 09:20
  • yes Kiwa, their comments are right. If the question was reopened I could post an answer. I had typed it already, but the question was closed by the time i was ready to submit it. Or ask another question, i can answer it there. – Harshith Rai Nov 15 '18 at 09:29
  • @Rai I don't think we need yet another duplicate of this question. OP, I've added more links to duplicate questions. – melpomene Nov 15 '18 at 09:42
  • Yes @melpomene.. i agree... – Harshith Rai Nov 15 '18 at 10:25

0 Answers0