Solved

Character Array Declarations

Posted on 2006-11-15
5
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 opening and reading files in the C programming language.

751 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