String is used uninitialized [duplicate]
This question already has an answer here:
Update (int) variable in C inside a function [duplicate]
5 answers
Change string with malloc in other function
2 answers
Passing Char pointer in C function [duplicate]
1 answer
How do I modify a pointer that has been passed into a function in C?
5 answers
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 %sn", 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 %sn", 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
c string char malloc strcpy
marked as duplicate by mch, melpomene
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 9:16
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 6 more comments
This question already has an answer here:
Update (int) variable in C inside a function [duplicate]
5 answers
Change string with malloc in other function
2 answers
Passing Char pointer in C function [duplicate]
1 answer
How do I modify a pointer that has been passed into a function in C?
5 answers
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 %sn", 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 %sn", 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
c string char malloc strcpy
marked as duplicate by mch, melpomene
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 9:16
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
What is unclear about the errors?test_function
takes 2 arguments, you call it with 3. There is no variablemy_str
in main, you only havemy_intro
andmy_name
.
– mch
Nov 15 '18 at 9: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 9: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 9:12
@Kiwa The fundamentals of pass-by-value don't change depending on the type of your variables.
– melpomene
Nov 15 '18 at 9:16
@melpomene I've edited my question with the logic of the duplicate.
– Kiwa
Nov 15 '18 at 9:17
|
show 6 more comments
This question already has an answer here:
Update (int) variable in C inside a function [duplicate]
5 answers
Change string with malloc in other function
2 answers
Passing Char pointer in C function [duplicate]
1 answer
How do I modify a pointer that has been passed into a function in C?
5 answers
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 %sn", 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 %sn", 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
c string char malloc strcpy
This question already has an answer here:
Update (int) variable in C inside a function [duplicate]
5 answers
Change string with malloc in other function
2 answers
Passing Char pointer in C function [duplicate]
1 answer
How do I modify a pointer that has been passed into a function in C?
5 answers
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 %sn", 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 %sn", 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
This question already has an answer here:
Update (int) variable in C inside a function [duplicate]
5 answers
Change string with malloc in other function
2 answers
Passing Char pointer in C function [duplicate]
1 answer
How do I modify a pointer that has been passed into a function in C?
5 answers
c string char malloc strcpy
c string char malloc strcpy
edited Nov 15 '18 at 9:21
melpomene
61.6k54994
61.6k54994
asked Nov 15 '18 at 9:03
KiwaKiwa
226
226
marked as duplicate by mch, melpomene
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 9:16
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by mch, melpomene
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 9:16
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
What is unclear about the errors?test_function
takes 2 arguments, you call it with 3. There is no variablemy_str
in main, you only havemy_intro
andmy_name
.
– mch
Nov 15 '18 at 9: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 9: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 9:12
@Kiwa The fundamentals of pass-by-value don't change depending on the type of your variables.
– melpomene
Nov 15 '18 at 9:16
@melpomene I've edited my question with the logic of the duplicate.
– Kiwa
Nov 15 '18 at 9:17
|
show 6 more comments
What is unclear about the errors?test_function
takes 2 arguments, you call it with 3. There is no variablemy_str
in main, you only havemy_intro
andmy_name
.
– mch
Nov 15 '18 at 9: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 9: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 9:12
@Kiwa The fundamentals of pass-by-value don't change depending on the type of your variables.
– melpomene
Nov 15 '18 at 9:16
@melpomene I've edited my question with the logic of the duplicate.
– Kiwa
Nov 15 '18 at 9:17
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 9:06
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 9:06
1
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 9:07
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 9: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 9:12
@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 9:12
@Kiwa The fundamentals of pass-by-value don't change depending on the type of your variables.
– melpomene
Nov 15 '18 at 9:16
@Kiwa The fundamentals of pass-by-value don't change depending on the type of your variables.
– melpomene
Nov 15 '18 at 9:16
@melpomene I've edited my question with the logic of the duplicate.
– Kiwa
Nov 15 '18 at 9:17
@melpomene I've edited my question with the logic of the duplicate.
– Kiwa
Nov 15 '18 at 9:17
|
show 6 more comments
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
What is unclear about the errors?
test_function
takes 2 arguments, you call it with 3. There is no variablemy_str
in main, you only havemy_intro
andmy_name
.– mch
Nov 15 '18 at 9: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 9: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 9:12
@Kiwa The fundamentals of pass-by-value don't change depending on the type of your variables.
– melpomene
Nov 15 '18 at 9:16
@melpomene I've edited my question with the logic of the duplicate.
– Kiwa
Nov 15 '18 at 9:17