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

  • Home
  • SEARCH
  • 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 6824591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:55:11+00:00 2026-05-26T21:55:11+00:00

Possible Duplicate: What is the difference between char s[] and char *s in C?

  • 0

Possible Duplicate:
What is the difference between char s[] and char *s in C?
Do these statements about pointers have the same effect?

All this time I thought that whenever I need to copy a string(either literal or in a variable) I need to use strcpy(). However I recently found out this:

char a[]="test";

and this

char *a="test";

From what I understand the second type is unsafe and will print garbage in some cases. Is that correct? What made me even more curious is why the following doesn’t work:

char a[5];
a="test";

or this

char a[];
a="test";

but this works however

char *a;
a="test";

I would be greatful if someone could clear up things a bit.

  • 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-26T21:55:12+00:00Added an answer on May 26, 2026 at 9:55 pm

    Let’s examine case by case:

    char a[]="test";
    

    This tells the compiler to allocate 5 bytes on the stack, put 't' 'e' 's' 't' and '\0' on it. Then the variable a points to where 't' was written and you have a pointer pointing to a valid location with 5 available spaces. (That is if you view a as a pointer. In truth, the compiler still treats a as a single custom type that consists of 5 chars. In an extreme case, you can imagine it something like struct { char a, b, c, d, e; } a;)

    char *a="test";
    

    “test” (which like I said is basically 't' 'e' 's' 't' and '\0') is stored somewhere in your program, say a “literal’s area”, and a is pointing to it. That area is not yours to modify but only to read. a by itself doesn’t have any specific memory (I am not talking about the 4/8 bytes of pointer value).

    char a[5];
    a = "test";
    

    You are telling the compiler to copy the contents of one string over to another one. This is not a simple operation. In the case of char a[] = "test"; it was rather simple because it was just 5 pushes on the stack. In this case however it is a loop that needs to copy 1 by 1.

    Defining char a[];, well I don’t think that’s even possible, is it? You are asking for a to be an array of a size that would be determined when initialized. When there is no initialization, it’s just doesn’t make sense.

    char *a;
    a = "test";
    

    You are defining a as a pointer to arrays of char. When you assign it to "test", a just points to it, it doesn’t have any specific memory for it though, exactly like the case of char *a = "test";

    Like I said, assigning arrays (whether null-terminated arrays of char (string) or any other array) is a non-trivial task that the compiler doesn’t do for you, that is why you have functions for it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C - Difference between char var[] and char *var? I have written
Possible Duplicate: What is the difference between char a[] = “string” and char *p
Possible Duplicate: difference between string object and string literal Let's say I have two
Possible Duplicate: Difference between int[] array and int array[] I was sure that this
Possible Duplicate: difference between destructor and garbage collector Recently i asked this question ,
Possible Duplicate: Difference between a Structure and a Union in C I see this
Possible Duplicate: Difference between lock(locker) and lock(variable_which_I_am_using) In all of the thread-safe code examples
Possible Duplicate: What is difference between instantiating an object using new vs. without This
Possible Duplicate: Difference between pointer variable and reference variable in C++ I saw this
Possible Duplicate: Difference between using var and not using var in JavaScript Hi All

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.