Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7684331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:00:05+00:00 2026-05-31T19:00:05+00:00

I have an array of 4 characters, for example: char char_array[10]; char_arr[0] = ‘a’;

  • 0

I have an array of 4 characters, for example:

char char_array[10];

char_arr[0] = 'a';
char_arr[1] = 'b';
char_arr[2] = 'c';
char_arr[3] = 'd';
char_arr[4] = '\0';

and I have a structure:

typedef struct{
int my_int;
char *my_string;
} my_struct_t;

my_struct_t my_struct;  

my_struct.my_string = malloc(10);

I want to assign the char array to the string my_struct.my_string
How to I do this? I tried the following:

Attempt: 1

my_struct.my_string = malloc(10) 
my_struct.my_string[0] = char_arr[2];
my_struct.my_string[1] = char_arr[2];
my_struct.my_string[2] = '\0';

Attempt: 2

strcpy(my_struct.my_string, char_arr);

Both fail i.e the destination is empty (compilation succeeds). Why does the above fail and how can I overcome this?

I have both the struct and the char array in stack since I don’t want these once I exit the function. I have allocated memory to my_struct.my_string before assigning it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:00:06+00:00Added an answer on May 31, 2026 at 7:00 pm

    You can do it two ways:

    Assign the pointer to the character array

    Once the function exits the char_array[10] which the pointer points to will become invalid. The compiler will produce no error, but eventually the program will likely crash because you will overwrite data for another function, or read the wrong data because another function overwrote it with something else. You should google about how the stack works on your computer architecture.

        char                  char_array[10];
        my_struct_t           var;
    
        var.my_string = &array[0];
    
        var.my_string[3] = 'a';
    
        if (var.my_string[0] == char_array[0])
            printf("See, changes using the pointer effect char_array.\n");
        else
            printf("This will never happen.\n");
    

    Allocate memory and copy the character array

    I used for loops to help make it easier to understand exactly how it works. Once the current function exits the pointer will still point to valid memory if you happen to keep it around somehow.

        char                  char_array[10];
        my_struct_t           var;
        int                   l;
    
        char_array[0] = 'a';
        char_array[1] = 0;
    
        // l = strlen(&char_array[0])
        for (l = 0; char_array[l] != 0; ++l);
    
        var.my_string = (char*)malloc(sizeof(char) * l);
    
        // strcpy(var.my_string, &char_array[0]);
        for (l = 0; char_array[l] != 0; ++l)
            var.my_string[l] = char_array[l];
    
        var.my_string[0] = 'a';
    
        if (var.my_string[0] != char_array[0])
            printf("See, changes using the pointer DO NOT effect char_array.\n");
        else
            printf("This will never happen.\n");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a char array buffer that I am using to store characters that
so if I have an array of characters char[] chars = new char[]{'f','a','d','e','c','b'}; and
Say I have an array of arbitrary size holding single characters. I want to
I have a simple function which takes an array of characters as an argument,
I have seen tutorials that are using an array of characters in order to
I have an array of char pointers of length 175,000. Each pointer points to
I have the following class (I've trimmed irrelevant stuff): class Example { private: char*
I have a character array and I'm trying to figure out if it matches
I have a array containing japanese caracters as well as normal. How do I
Is a string actually a character array (is-a), or does it have a character

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.