Solved

Character Array Declarations

Posted on 2006-11-15
5
208 Views
Last Modified: 2012-05-05
I realize that i can use :
char *filename;
filename = "aFile.txt";

and

char filename[20] = "aFile.txt";

There do get the same char array right? If so, when do you know when to use each one.

Are there any limitations to either? How does the pointer method realize the size of the array?
0
Comment
Question by:bbcac
  • 2
  • 2
5 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 17949310

Hi bbac,

The pointer method can not determine the size of the array.  It can determine the size of the string, but not the underlying buffer.

Which one is appropriate in a situation depends a lot on what you want to do with it.


--  Reserve a buffer in the program's variable block and preset it to "aFile.txt".

char filename[20] = "aFile.txt";

--  Reserve a buffer in the program's CONSTANTS block and preset it to "aFile.txt".  Set variable to the address

char *filename = "aFile.txt";


In the first example, filename is an array and behaves just like any other array.  The second example is subtlely different in that filename points to a string in read-only memory.  Try to change the value (perhaps by capitalizing the first character) and the program will abort.


So that's the primary difference.  One puts the buffer in the variables block and initializes it to your value.  The other puts a pointer in the variables block and references memory in the constants block.



Good Luck,
Kent
0
 

Author Comment

by:bbcac
ID: 17950807
thanks
0
 

Expert Comment

by:karana
ID: 17953699
hi Kent ,


>>>>>>>>>>>>>" The pointer method can not determine the size of the array.  It can determine the size of the string, but not the underlying buffer. "

               char *ptr  = "hello" ;

 ptr points to a a location in  DS .  ptr is unaware of the size of the string . U can see
                 printf("%d" ,sizeof(ptr));
       
 but when u try o print it
                              printf( "%s" , ptr) , this printf will actually search for  '/0' ,so it will print
the whole text  "hello "


      The allocation in DS for eg:
                      DS: 0x100     h  e l  l  o
                      DS:0x105     '\0'
 is done by the compiler .It purely depends on compiler .  So  i disagree with
           " It can determine the size of the string " 

 

0
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 125 total points
ID: 17955825

Hi karana,

A pointer contains no information about the string except its starting location.  Note that the compiler has little need for the length of the string except for reserving storage for the string.

The sizeof() macro will return the size of the object.  When the pointer method is used sizeof() will return the size of a pointer.  Probably 4.  When the array method is used sizeof() will return the size of the character array, not the length of the string.

The C string utilities can determine the length using strlen().  It doesn't matter how the string is defined.


So I stand by the statement that, "The pointer method can not determine the size of the array.  It can determine the size of the string, but not the underlying buffer".

Kent
0
 

Expert Comment

by:karana
ID: 17963359

thank you .
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question