Passing string to struct in C

-1

I am complete beginner in C and need help with saving strings in structs.

(I tried multiple ways to have it done but program prints nothing or i got (0xC0000005) or program shuts down.. (I tried using pointer as a parameter but effect is the same-or using scanf straightly for text.line and etc ). I would be glad if somebody explained me simply how it should be done - I got feeling that i still dont fully understand idea of pointers and thats the problem ;q.

typedef struct label{
    char *line;
}label;

void save_line(label text){

  printf("Write your name\n");
  char *helper=malloc(30 * sizeof *helper);

  scanf("%s", helper);
  strcpy(text.line, helper);


}

void main(){
   label text;
   save_line(text);
   printf("%s", text.line);

}
c
pointers
struct
asked on Stack Overflow Feb 5, 2019 by user3231387

4 Answers

2

When you pass a parameter to a function in C it creates a copy of it, so the text variable you have in main is not the same as the one you have in save_line.

You need to pass a pointer the text to save_line function, like so:

void save_line(label *text) {

  printf("Write your name\n");
  char *helper=malloc(30 * sizeof *helper);

  scanf("%s", helper);
  text->line = helper;

}

And in main:

void main(){
  label text;
  save_line(&text);
  printf("%s", text.line);
}
answered on Stack Overflow Feb 5, 2019 by Leo
2

There is no space to allocate your string in line, because it is just a no initialized pointer, so you will have to reserve some memory for it. Furthermore, your function is allocating memory which is not being deallocated (and regarding helper, you wouldn't need two different bunchs of memory for the same string).

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

const uint8_t MAX_NAME_SIZE = 20;

typedef struct label{
    char *line;
}label;

void save_line(label text){
    printf("Write your name (max len %u chrs):\n",(MAX_NAME_SIZE-1));
    scanf("%s", text.line);
}

void main(){
    label text;
    text.line = malloc(MAX_NAME_SIZE);
    save_line(text);
    printf("%s", text.line);
    free(text.line);
}

I prefer to allocate and deallocate memory in the same scope, to avoid memory leaks (inside the same function, inside the main, etc), so in order to keep your structure, for the example the memory is being managed in the main. You should think about your design, and analyse the scope that label text; should have.

answered on Stack Overflow Feb 5, 2019 by Jose • edited Feb 5, 2019 by Jose
1

Your code is wrong at many levels:

You probably want something like this:

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

typedef struct label {
  char *line;
}label;

void save_line(label *text) {   // use label *ttext instead of label text
  printf("Write your name\n");
  text->line = malloc(30 * sizeof *text->line);   // no need for strcpy here anyway 
  scanf("%s", text->line);
}

int main() {               // main should return int *
  label text;
  save_line(&text);        // pass the pointer to text, not just text
  printf("%s", text.line);
  free(text.line);         // free allocated memory
}

There is still room for improvement though, e.g. scanf("%s"), ... is dangerous because if the user types too many characters, you'll get a buffer overflow.

*read this: What should main() return in C and C++?

answered on Stack Overflow Feb 5, 2019 by Jabberwocky • edited Feb 5, 2019 by Jabberwocky
0

strcpy only copies the thing pointed to by the source to the destination. It does not allocate space in the destination for the stuff being copied.

If your platform has strdup you can use that instead:

text.line = strdup(helper);

If not, allocate some space in text.line before you strcpy

text.line = malloc(strlen(helper) + 1); // +1 for the null byte at the end
 strcpy(text.line, helper);
answered on Stack Overflow Feb 5, 2019 by JeremyP

User contributions licensed under CC BY-SA 3.0