Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Character Array Declarations

Posted on 2006-11-15
5
Medium Priority
?
211 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 46

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 46

Accepted Solution

by:
Kent Olsen earned 500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
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 opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

719 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