Jump to content
Sign in to follow this  
torels

Pointers and Strings

Recommended Posts

torels

Hi there

I am learning C++ (or trying to do that... XD) but I have a BIG perplexity on a piece of code I wrote

how 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

Share this post


Link to post
Share on other sites
wraithdu

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 by wraithdu

Share this post


Link to post
Share on other sites
torels

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

Share this post


Link to post
Share on other sites
monoceres

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 by monoceres

Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites
wraithdu

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.

Share this post


Link to post
Share on other sites
wraithdu

LOL, that's hilarious. ^_^

Share this post


Link to post
Share on other sites
torels

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 XD

I'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

Share this post


Link to post
Share on other sites
cppman

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). ^_^

Share this post


Link to post
Share on other sites
monoceres

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 by monoceres

Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites
system24

So you can't use pointer-based strings for input. Everytime I do that this happens...

Posted Image


[center]It's a question of mind over matter, if I don't mind, it doesn't matter.[/center]

Share this post


Link to post
Share on other sites
Richard Robertson

Pointer based strings are the only way to perform input. You have to have a buffer to receive to.

Share this post


Link to post
Share on other sites
system24

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]

Share this post


Link to post
Share on other sites
Valik

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.

Share this post


Link to post
Share on other sites
system24

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 by system24

[center]It's a question of mind over matter, if I don't mind, it doesn't matter.[/center]

Share this post


Link to post
Share on other sites
Richard Robertson

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.

Share this post


Link to post
Share on other sites
jvanegmond

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.

Share this post


Link to post
Share on other sites
dexto

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.

Share this post


Link to post
Share on other sites
Richard Robertson

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.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×