torels Posted April 22, 2009 Share Posted April 22, 2009 Hi thereI am learning C++ (or trying to do that... XD) but I have a BIG perplexity on a piece of code I wrotehow come is this not working ?#include <iostream> using namespace std; int Dimension(char* Ptr) { int i=0; do { i++; } while (Ptr[i]!='\0'); return i; } int main() { char* String; cout<<"Scrivi una stringa"<<endl; cin>>String; //HERE IS THE PROBLEM... I DON'T KNOW WHY... cout<<"Al contrario: " << strrev(String) << endl; return 0; }It only worked like this:#include <iostream> using namespace std; int Dimension(char* Ptr) { int i=0; do { i++; } while (Ptr[i]!='\0'); return i; } int main() { char* String; char StringArray[100000]; cout<<"Scrivi una stringa"<<endl; cin.getline(StringArray, 100000); String=StringArray; cout<<"Al contrario: " << strrev(String) << endl; return 0; }My question is... isn't there a way to get strings (or any other input) WITHOUT using an Array before ? (not using #include <String>... I'm trying to learn pointers... that's the whole point thanks in advance Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org Link to comment Share on other sites More sharing options...
wraithdu Posted April 22, 2009 Share Posted April 22, 2009 (edited) No. Just declaring 'char *string;' only declares a POINTER to a char array (currently a pointer to VOID because you haven't declared the array). You must allocate space for the string, the array, before trying to store anything there. You also cannot use just 'cin >> var;' since it won't accept strings with spaces. char buff[100]; cout << "type something" << endl; cin.getline(buff, 100); cout << "you typed: " << buff << endl; Edited April 22, 2009 by wraithdu Link to comment Share on other sites More sharing options...
torels Posted April 22, 2009 Author Share Posted April 22, 2009 ah ok now everything's clearer thanks alot btw... I knew about the cin.getline() func and... I have another question... How Do I Redimension an Array... So let's say... if I declare buff[100] but the input is only 5 chars and I want to free Memory by redimensioning the array to 4... how do I do that ? Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org Link to comment Share on other sites More sharing options...
monoceres Posted April 22, 2009 Share Posted April 22, 2009 (edited) How Do I Redimension an Array... So let's say... if I declare buff[100] but the input is only 5 chars and I want to free Memory by redimensioning the array to 4... how do I do that ? You can't redimension an array. If you want to beeing able to allocate and delete memory during runtime you have to use the new and delete keywords. http://www.cplusplus.com/doc/tutorial/dynamic/ Edited April 22, 2009 by monoceres Broken link? PM me and I'll send you the file! Link to comment Share on other sites More sharing options...
wraithdu Posted April 22, 2009 Share Posted April 22, 2009 You cannot. Standard arrays in C++ are static once declared. You're talking about dynamic memory (new and delete operators). You'd have to create your new array, copy the old values into the new array, and delete the old array.http://www.cplusplus.com/doc/tutorial/dynamic/I think it's time for a C++ book for you. Link to comment Share on other sites More sharing options...
wraithdu Posted April 22, 2009 Share Posted April 22, 2009 LOL, that's hilarious. Link to comment Share on other sites More sharing options...
torels Posted April 22, 2009 Author Share Posted April 22, 2009 You cannot. Standard arrays in C++ are static once declared. You're talking about dynamic memory (new and delete operators). You'd have to create your new array, copy the old values into the new array, and delete the old array.http://www.cplusplus.com/doc/tutorial/dynamic/I think it's time for a C++ book for you.Yeah maybe XDI'm not quite understanding it...It's... let's say... complicated! Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org Link to comment Share on other sites More sharing options...
cppman Posted April 25, 2009 Share Posted April 25, 2009 No. Just declaring 'char *string;' only declares a POINTER to a char array (currently a pointer to VOID because you haven't declared the array).I'm not exactly sure what you mean here. Whether you initialize the pointer or not, it is still a pointer to a character. If you mean the pointer is initialized to zero, that is not true. Variables (that includes pointers) will not be initialized to any specific value - it'll take the value of what was previously in that memory address (which sometimes happens to be 0). Miva OS Project Link to comment Share on other sites More sharing options...
monoceres Posted April 25, 2009 Share Posted April 25, 2009 (edited) I'm not exactly sure what you mean here. Whether you initialize the pointer or not, it is still a pointer to a character. If you mean the pointer is initialized to zero, that is not true. Variables (that includes pointers) will not be initialized to any specific value - it'll take the value of what was previously in that memory address (which sometimes happens to be 0). I think he just means it doesn't point to any usable data. Edited April 25, 2009 by monoceres Broken link? PM me and I'll send you the file! Link to comment Share on other sites More sharing options...
system24 Posted April 28, 2009 Share Posted April 28, 2009 So you can't use pointer-based strings for input. Everytime I do that this happens... [center]It's a question of mind over matter, if I don't mind, it doesn't matter.[/center] Link to comment Share on other sites More sharing options...
Richard Robertson Posted April 28, 2009 Share Posted April 28, 2009 Pointer based strings are the only way to perform input. You have to have a buffer to receive to. Link to comment Share on other sites More sharing options...
system24 Posted April 29, 2009 Share Posted April 29, 2009 So, both char *str and char str[25] are pointer-based strings? [center]It's a question of mind over matter, if I don't mind, it doesn't matter.[/center] Link to comment Share on other sites More sharing options...
Valik Posted April 29, 2009 Share Posted April 29, 2009 So, both char *str and char str[25] are pointer-based strings?Sigh. Read a book or good FAQ. The terminology being used in this thread is confusing at best. Link to comment Share on other sites More sharing options...
system24 Posted April 29, 2009 Share Posted April 29, 2009 (edited) Yeah, confusing at best. A pointer-based string in C++ is an array of characters ending in the null character ('\0'), which marks where the string terminates in memory. Edited April 29, 2009 by system24 [center]It's a question of mind over matter, if I don't mind, it doesn't matter.[/center] Link to comment Share on other sites More sharing options...
Richard Robertson Posted April 29, 2009 Share Posted April 29, 2009 That's not even always true. I've had strings in C++ without a null terminator. I tracked the length of the string myself and was only using the string in my own methods. Link to comment Share on other sites More sharing options...
jvanegmond Posted April 30, 2009 Share Posted April 30, 2009 To avoid confusion, I think it would be better to call that a character array instead of a string. Although, it is a string ofcourse. github.com/jvanegmond Link to comment Share on other sites More sharing options...
dexto Posted May 18, 2009 Share Posted May 18, 2009 Would it be easer to work with "#include <string>"? and declare cin variable as "string". I think later you can refer to it as a char pointer or array pointer anyway. Link to comment Share on other sites More sharing options...
Richard Robertson Posted May 18, 2009 Share Posted May 18, 2009 cin is an object of type istream. It's not a string. Also I have no idea what that second part was supposed to mean. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now