• C

Is string constant address ?

typedef char * string_t;

string_t msg=“No more room”;  /* how much space are we reserving here?  Please explain this statement. */

Open in new window


I believe msg is a pointer to a char.  msg value should be address.  Why is it string ?
LVL 1
naseeamAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
msg is string_t because it was declared to be string_t
"No more room" is a constant address.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
naseeamAuthor Commented:
How much space is reserved?  Four bytes because it is constant address ?
0
naseeamAuthor Commented:
char s[] = "hi";

Is "hi" constant address ?

Is s pointer to char ?

What is the value of s,  "hi"  ?
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

ozoCommented:
What do you mean by reserved?
The address takes sizeof(char *) bytes
The "No more room" takes 13 bytes, but there may be padding.
0
Karrtik IyerSoftware ArchitectCommented:
The below program should answer all your questions, please let me know if something is not yet clear.
#include <iostream>
#include <string.h>

using namespace std;

int main()
{
    typedef char * string_msg;
    string_msg msg = "No more room";
    cout << "Base Address of the character array:  " <<&msg << endl;
    cout << "String: " << msg << endl;
    cout << "Length: " << strlen(msg) << endl;
    return 0;
}
0
naseeamAuthor Commented:
By reserve I mean how many bytes are allocated in following declaration or definition ?

string_t msg=“No more room”;
0
Karrtik IyerSoftware ArchitectCommented:
13 bytes to be precise.
0
ozoCommented:
#include <stdio.h>
int main(){
  typedef char * string_t;
  printf("sizeof(string_t)=%d\n",sizeof(string_t));
  char msg[]="No more room";
  printf("sizeof(msg)=%d\n",sizeof(msg));
}
0
evilrixSenior Software Engineer (Avast)Commented:
Please note that ozo has provided the core of the answer you are looking for and the following just elaborates and tries to provide a little more explanation for you.

>> I mean how many bytes are allocated in following declaration or definition
sizeof(“No more room”)

Open in new window

tells you how many bytes make up that string constant

sizeof(msg)

Open in new window

tells you the size of the pointer that is used to point to that string constant

In both cases, it is dependant upon the compiler and the system architecture and it's not something that can be determined just looking at the code. The C Standard is very flexible with it's wording on the size of intrinsic types and leaves it up to the implementation to decide their size (within certain parameters).

>> How much space is reserved?  Four bytes because it is constant address ?
Depends upon the system architecture, your compiler and whether your program is compiled as 32 bit, 64 bit or other.

>> char s[] = "hi";
>> Is "hi" constant address ?

The address of "hi" will not change as it will be set by the compiler at compile time (it'll actually be "burnt" into the binary's data segment and will always have a constant address - although the actual address may differ each time the program is run, it will not change for the duration of the runtime.

>> Is s pointer to char ?
In this case, no. In this case "s" is an array. To be a pointer you'd have to define it thus:

char * s = "hi";

Open in new window


Arrays automatically decompose to point types (for example when passed to functions that take pointers of the same type) but an array is NOT a pointer - the compiler knows they are two distinct types.

>> What is the value of s,  "hi"  ?
In this case, the array s will be initialised with the constant string "hi". It does not point to the constant string; however, it is actually a copy of the constant string.

char s[] = "hi"; // this is an array that is assigned a copy of the constant string "hi"
char * s = "hi"; // this is a pointer that points to the constant string "hi"

Open in new window


Additionally...

>> 13 bytes to be precise.
That depends on what the size is of "char". It doesn't have to be a byte (according to the C Standard, it just has to be appropriate to represent the character set of the implementation).

>> The below program should answer all your questions
I'm not sure I actually agree with that assertion if I am being completely honest. For example, strlen returns the length of the string and not the amount of memory the string uses! The two thing are very different.
0
ozoCommented:
what the size is of "char".
in C, sizeof(char)==1
and "byte" as used in the standard means the same as char.
If you want to know the size of that, you can check CHAR_BIT in <limits.h>

strlen("No more room")==12 because it does not count the terminating null char
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.