I'm trying to Initialize a string in Initialize then pass it to int main() for screen output, but it seems that the strings that are initialized have become corrupted.
Headers
#include<stdio.h>
#include<stdlib.h>
Initialize
void
Initialize(char* STRINGs)
{
STRINGs = malloc(sizeof(char)*5);
STRINGs = "hello" ;
printf("1: %s\n",STRING);
}
Main
int
main (char* STRINGs)
{
Initialize(STRINGs);
//The program stops working when it reaches this section
printf("2: %s",STRINGs);
return 0;
}
You can use:
void Initialize(char** STRING)
Instead:
void Initialize(char* STRINGs)
because you want to change the the address to which STRING points
Also you have wrong prototype of main
Try:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void Initialize(char** STRING)
{
*STRING = malloc(6);
strcpy(*STRING, "hello");
printf("1: %s\n",*STRING);
}
int main (int argc, char *argv[])
{
char* STRING;
Initialize(&STRING);
printf("2: %s\n",STRING);
free(STRING);
return 0;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments