Solved

Character Array Declarations

Posted on 2006-11-15
5
204 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 conditional statements in the C programming language.

760 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

17 Experts available now in Live!

Get 1:1 Help Now