Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Pointers to chars
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ScaryRat




PostPosted: Sun Aug 29, 2010 8:27 pm   Post subject: Pointers to chars

I understand pointers and arrays, but this is confusing:

char * stuff = "stuff";

I believe this would be valid, but how could a pointer itself (stuff) be defined as a string? I thought the only definition of pointers are supposed to be addresses?

void FUNCTION (char * other_stuff);
FUNCTION("other stuff");

Once again, a string of text is passed onto a pointer. Why would people do this instead of passing the argument onto a string? And if I were to do this :

cout << *other_stuff;

what would other_stuff even point to?

Thanks for any help
Sponsor
Sponsor
Sponsor
sponsor
DtY




PostPosted: Sun Aug 29, 2010 9:19 pm   Post subject: RE:Pointers to chars

A string is an array. An array is a pointer.

When you do char *stuff = "stuff", what happens is that a pointer is created, pointing to the "s" at the beginning of stuff (this is part of the program that is loaded into memory, so you're not allowed to edit this text), and then assigns it to the variable named stuff.

Like I said at first, a string is an array, and an array is a pointer. When you use an array, you are using a pointer that points to the first value in the array. If you want the second value in the array, you would access the address right after the first item in the array.

A char pointer is not necessarily a string, it could be, just that, a character pointer.

I'm not sure what you mean by "passing the argument onto a string", but I assume you mean passing the string directly, instead of a pointer to the string, the answer is because C doesn't let you, this is because strings are arrays, which cannot be passed to functions.

If you did cout << *other_stuff; it would output the first character in the string. When you access *other_stuff, it accesses the value at the pointer, in the case of an array (slash string), this is the first item in the array (exactly the same as other_stuff[0]).

Keep in mind that there are two kinds of strings in C++.

What you are working with are called C strings, they're called this because they're the only kind of strings in C.

In C++ however, there is a more powerful string class, you can read about that here: http://www.cplusplus.com/reference/string/

And now, something even more confusing. In C, there is a header for working with C strings called "strings.h". In C++, you can still access this (either as "strings.h" or "cstrings" (only the first will work in C)), but if you want to work with c++ strings, you need to include the "string" header.

So,
code:
//C strings (either of these)
#include <strings.h>
#include <cstrings>
//C++ strings
#include <string>


Just be careful you include the right header, or some stuff wont work.
ScaryRat




PostPosted: Mon Aug 30, 2010 8:34 am   Post subject: RE:Pointers to chars

Thx! I would give you karma but i dont have enough posts. xD
LeeShane




PostPosted: Wed Nov 17, 2010 3:33 am   Post subject: RE:Pointers to chars

character pointers are probably the most common pointers in C++.
Make sure you understand that in the lines:

char *Ptr;
Ptr = "now is the time";
Ptr = "hello, world";
OneOffDriveByPoster




PostPosted: Wed Nov 17, 2010 12:18 pm   Post subject: Re: RE:Pointers to chars

DtY @ Sun Aug 29, 2010 9:19 pm wrote:
A string is an array. An array is a pointer.
Not exactly of course. An array is an area of memory capable of storing its elements (contiguously). The name of an array, used in certain contexts, would act like a pointer to its first element. For example, a global array may be represented as a symbol in the object file with a defined size in the data section. A use of the array may involve loading the address of the symbol, which is then used for indirection. While the array indexing operations are indeed "syntactic sugar", the ability to declare arrays is not.
OneOffDriveByPoster




PostPosted: Wed Nov 17, 2010 12:25 pm   Post subject: Re: RE:Pointers to chars

DtY @ Sun Aug 29, 2010 9:19 pm wrote:
And now, something even more confusing. In C, there is a header for working with C strings called "strings.h". In C++, you can still access this (either as "strings.h" or "cstrings" (only the first will work in C)), but if you want to work with c++ strings, you need to include the "string" header.
Probably just a typo, but the C Standard header is <string.h>, accessible using <cstring> in C++ (along with the ::std namespace bit). "strings.h" is header in POSIX.

Edit: I did not notice that DtY's post is from August... sorry.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: