Solved

Character Array Declarations

Posted on 2006-11-15
5
205 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:Kdo
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:
Kdo 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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now