How do I pass a string array from a function to main

I I

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;
}
Martín Muñoz del Río

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how do i pass an array with a pointer into a function?

From Dev

how do i pass an array with a pointer into a function?

From Dev

How do I pass in a parameter to a function in an array?

From Dev

how do I write a function to form a string from array?

From Dev

How Do I Pass an Array of Strings Query String with the JdbcTemplate Query For Object Function?

From Dev

How Do I Pass an Array of Strings Query String with the JdbcTemplate Query For Object Function?

From Dev

How do I return a value from the main function in MIPS?

From Dev

How do I change an array used from main

From Dev

How do I pass a Java function a String[] argument?

From Dev

How do I pass in a string to get populated by getJSON's function?

From Dev

How do I pass a string to a function requiring an Object?

From Dev

How do I pass a Java function a String[] argument?

From Dev

How do i pass function parameter as string in fish shell

From Dev

How do I pass an array or list as a parameter to a PowerShell function?

From Dev

How do I pass an array to a function in Rust and change its content?

From Dev

How do I pass a list of values to a function expecting an array?

From Dev

How do I pass an address of array of structs to a function?

From Dev

How do I pass an array or list as a parameter to a PowerShell function?

From Dev

How do I assign a string from an array to a variable in C (from a function)?

From Dev

How do I create and pass string array to a sub in Excel VBA?

From Dev

How do I pass $(this) to a function?

From Dev

With C++ how do I Pass a value from a void function?

From Dev

How do I pass values from my express middleware function?

From Dev

How do I pass and get an String value from different activity?

From Dev

How do I pass an array from Perl to an Expect script?

From Dev

How do I pass program-argument to main function in running spark-submit with a JAR?

From Dev

How do I pass my struct values into my print function and then call it in main? - C Language

From Dev

How do I pass the array to the main method after calculating the sum of values?

From Dev

how to pass array used in function to the dynamically allocated array of main?

Related Related

  1. 1

    how do i pass an array with a pointer into a function?

  2. 2

    how do i pass an array with a pointer into a function?

  3. 3

    How do I pass in a parameter to a function in an array?

  4. 4

    how do I write a function to form a string from array?

  5. 5

    How Do I Pass an Array of Strings Query String with the JdbcTemplate Query For Object Function?

  6. 6

    How Do I Pass an Array of Strings Query String with the JdbcTemplate Query For Object Function?

  7. 7

    How do I return a value from the main function in MIPS?

  8. 8

    How do I change an array used from main

  9. 9

    How do I pass a Java function a String[] argument?

  10. 10

    How do I pass in a string to get populated by getJSON's function?

  11. 11

    How do I pass a string to a function requiring an Object?

  12. 12

    How do I pass a Java function a String[] argument?

  13. 13

    How do i pass function parameter as string in fish shell

  14. 14

    How do I pass an array or list as a parameter to a PowerShell function?

  15. 15

    How do I pass an array to a function in Rust and change its content?

  16. 16

    How do I pass a list of values to a function expecting an array?

  17. 17

    How do I pass an address of array of structs to a function?

  18. 18

    How do I pass an array or list as a parameter to a PowerShell function?

  19. 19

    How do I assign a string from an array to a variable in C (from a function)?

  20. 20

    How do I create and pass string array to a sub in Excel VBA?

  21. 21

    How do I pass $(this) to a function?

  22. 22

    With C++ how do I Pass a value from a void function?

  23. 23

    How do I pass values from my express middleware function?

  24. 24

    How do I pass and get an String value from different activity?

  25. 25

    How do I pass an array from Perl to an Expect script?

  26. 26

    How do I pass program-argument to main function in running spark-submit with a JAR?

  27. 27

    How do I pass my struct values into my print function and then call it in main? - C Language

  28. 28

    How do I pass the array to the main method after calculating the sum of values?

  29. 29

    how to pass array used in function to the dynamically allocated array of main?

HotTag

Archive