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 7182139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:42:25+00:00 2026-05-28T17:42:25+00:00

If I have a string: char s1[]=hello, how are you?; printf(%d\n,sizeof(s1)); it prints the

  • 0

If I have a string:

char s1[]="hello, how are you?";
printf("%d\n",sizeof(s1));

it prints the exact, right number of characters, 20. But if I have a string initialized by a pointer:

char *s2;
s2=(char*)malloc(sizeof(char));
strcpy(s2,s1);
printf("%d\n",sizeof(s2));

it prints the size of a pointer, which depends on the machine (on mine, 8).
So why is it 8 bytes for s2, and 20 bytes for s1, since they are the same string?

  • 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-28T17:42:26+00:00Added an answer on May 28, 2026 at 5:42 pm

    They are not the same string, or even the same type. char s2[] is an array, it has the size of the sum of the size of its elements. char *s2 is a pointer, it has the size of a pointer.

    sizeof() is (in C89) a compile-time operator that must know the size of it’s argument at compile time. Since a pointer can come from anywhere, sizeof() can’t know the size of the allocated memory, but an array is always given a size at compile time (except for C99’s VLA’s), and sizeof() can then return the size of that memory.

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

Sidebar

Related Questions

I have a string as const char *str = Hello, this is an example
I have a char pointer which would be used to store a string. It
I have a struct where a constant char string is defined and a pointer
Say I have the following: char* string = Hello, how are you?; Is it
I have a string(char*), and i need to find its underlying datatype such as
I have a string (char) and I want to extract numbers out of it.
Suppose I have a string char* str . I print it to the buffer
I have the following string: const char *str = \This is just some random
I have one method with one letter(string\char?) as argument and one word active at
I have 3 files: 1) cpf0.ml type string = char list type url =

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.