• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

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 ?
0
naseeam
Asked:
naseeam
  • 4
  • 3
  • 2
  • +1
5 Solutions
 
ozoCommented:
msg is string_t because it was declared to be string_t
"No more room" is a constant address.
0
 
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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now