0

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;
}
artm
  • 17,291
  • 6
  • 38
  • 54
I I
  • 15
  • 4
  • you need to learn strcpy and lot of string operations, pointers before malloc. – Jay Kumar R Feb 13 '16 at 06:12
  • 1
    The program starts from `main` function. So you should do the task you want in `main` by using another function. – Shafi Feb 13 '16 at 06:16
  • This `int main (char* STRINGs)` is not valid C. Either use `int main (void)` or `int main (int argc, char * argv[])` or `int main (int argc, char ** argv)`. – alk Feb 13 '16 at 15:40

5 Answers5

3

First, you have wrong prototype for int main (char* STRINGs), which must be either:

  • int main(), or
  • int main( int argc, char *argv[] )

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

As it stands, you can create a string inside your Initialize() then return a pointer to that string.

There are several issues in your Initialize() though.

Here's a suggestion to change:

char * 
Initialize()
{
    char *STRINGs = malloc(strlen("hello") + 1);  // <-- malloc must include an additional space for the NULL terminator.
    strcpy( STRINGs, "hello" );  // <-- NEVER use assignment for string type.
    printf("1: %s\n",STRINGs);
    return STRINGs;
}

Then your main() can be like this:

int main()
{
    char *str = Initialize();
    printf( "str = %s\n", str );
    return 0;
}

NOTE: do not forget to add #include <string.h>

artm
  • 17,291
  • 6
  • 38
  • 54
  • Why `sizeof(char)* (strlen("hello") + 1)`? As `sizeof(char)` is always 1, code could use `strlen("hello") + 1` or if trying got account for the size of the type, use `sizeof *STRINGs * (strlen("hello") + 1)`. – chux - Reinstate Monica Feb 13 '16 at 07:42
  • @chux `sizeof( char )` (instead of `1`) is preferred to make it explict. But I see this case it looks a bit cumbersome. I'm editing the code based on your comment - thanks. – artm Feb 13 '16 at 08:33
  • `char * Initialize()` should be `char * Initialize(void)`. – alk Feb 13 '16 at 15:36
3

You can use this code to initialize the string variable

  char * Initialize()
  {
    char* STRINGs="HELLO";

     printf("1: %s\n",STRINGs);

     return STRINGs;
  }

int main ()
{

 char *strings =Initialize();

 //The program stops working when it reaches this section
 printf("2: %s",strings);
 return 0;
}
Nutan
  • 778
  • 1
  • 8
  • 18
  • `char * Initialize()` should be `char * Initialize(void)`. – alk Feb 13 '16 at 15:36
  • An explanation why this works would suite the answer well. As it stands it does not help very much. – alk Feb 13 '16 at 15:43
  • Look at the question he asked . He want to initialize a variable to a string in a function. so according to the questions requirement my ans may satisfy the question. – Nutan Feb 14 '16 at 04:20
  • and plz explain why it should be `char * Initialize(void)` and not `char * Initialize()` – Nutan Feb 14 '16 at 04:21
  • Please see the current C-Standard on this. From the C11 Standard (draft) 6.11.7 (http://port70.net/~nsz/c/c11/n1570.html#6.11.7): "*The use of function definitions with separate parameter identifier and declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.*" – alk Feb 14 '16 at 06:49
1

Here's an answer. First, when allocating memory for any variable, it must be freed or you'll get some nasty system errors at some point or at the very least, a memory leak.

In the int main(), the declaration should ideally be int main(int argc, char* argv[]).

I also recommend allocating at least one more byte of memory just in case you create a string and a function you use later on requires a null character appended to it.

I fixed your code to make it work at its bare minimum.

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

char* Initialize(){
  char* string = malloc(sizeof(char)*6);
  strcpy(string,"hello");
  printf("1: %s\n",string);
  return string;
}

int main (int argc, char* argv[]){
  char *strings=Initialize();
  printf("2: %s\n",strings);
  free(strings);
  return 0;
}

For a shorter version of your code, I suggest this:

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

int main (int argc, char* argv[]){
  char* strings=malloc(6);
  strcpy(string,"hello");
  printf("2: %s\n",strings);
  free(strings);
  return 0;
}
Mike -- No longer here
  • 2,064
  • 1
  • 15
  • 37
1

For the sake of understanding, let us suppose STRINGs of main function is a.

And STRINGs of initialize function is b.

At first, in main a is pointing to some unknown location say u. When you pass this to the initialize function then b also starts pointing to the location u.

But, after the allocation of memory, b starts pointing to some other memory that was allocated by malloc say m.

Now you change the contents of memory m by use of b. But a is still pointing to the unknown location u.

So both the pointers are now pointing towards two different memory locations. So when you print contents where b is pointing it works perfectly and then you printf contents of a which has no specific location or may be null.

So, because of this your problem came.

And also, there is another error in your program that is in the printf of initialize function, it has given a parameter STRING which is undeclared....make it STRINGs.

Hope you will like the explanation. Its a little bit tricky.

Thanks :-)

Ahmed Akhtar
  • 1,444
  • 1
  • 16
  • 28
Harsh Dave
  • 329
  • 1
  • 2
  • 13
  • oh, I though void functions share variables with main and malloc simply expands the memory. this will clear some misconceptions. thank you – I I Feb 13 '16 at 08:56
  • Nicely explained. However "*`null`*" should read "*`NULL`*", as `null` is something different. – alk Feb 13 '16 at 15:44
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;
}
  • it worked! thank you – I I Feb 13 '16 at 08:57
  • The fact that it can work does not necessarily mean that it's a good solution. It's usually better to avoid pointer to pointer unless you have to - in this case you certainly don't – artm Feb 13 '16 at 10:09