Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

General Question

Status
Not open for further replies.

rjr9999

Programmer
Apr 7, 2001
183
US
Just a general question here...when dealing with strings, when, or why would it be more efficient to use a pointer, instead of an array...I personally prefer arrays...for example.

char string[40] (limits you to a 41 character string)
char *string (no limit, unless you malloc/calloc)

now if i wanted to rearange, organize, etc...it would be easier to do with an array, as apposed to a pointer.

The only real reason i see of using a pointed is when dealing with binary info...agree/dissagree? And why?

Rob
 
Generally, pointers are easier to use than arrays and make for more efficient code once you get comfortable with them. Sometimes, a pointer is the only reasonable option.

For strings, an array is good if you know the length of the string that will be stored beforehand and the length doesn't vary much and/or is small.

However, suppose that you're writing a general purpose function that processes lines in a file and you don't want to restrict the length of a line to an arbitrary limit. In this case, a pointer to char is the only way to go.

There are many other situations with strings where pointers are preferable but usually the decision hinges on what you know about the data beforehand. If you try to use arrays all the time, you'll quickly run into problems with the arbitrary limit you're placing on your data.

Another place where using pointers is preferable to arrays is in implementing abstract data types such as linked lists. There's no question that a pointer is the appropriate choice here.

In C, learning how to use pointers and using them judiciously is essential. Otherwise, there really isn't much reason to use C.

Russ
bobbitts@hotmail.com
 
Few more point,

If we use c[10] this will be processed like c+10 [any we know, c the starting address of the array ] internally, if we use the internal implementation directly in our program ..., now consider the speed.

In the example you said that there is no limit for the characters if you are not using malloc or calloc but If you declare a pointer variable have to use it to store addresses only. We should not use it directly, the pointer variable should point some arrays are dynamically allocated memory (using malloc or calloc) space.

Regards
Maniraja S


 
I would like to add

The Pointers facility is specially useful because of Dynamic Memory Allocation. This facility allows the programmer to increase the number of elements in an array (which is used in the form of pointers) during run time. So you do not need to allocate extra space by assuming the maximum possible limit of an array index.


Navin

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top