Jump to content

Pointers and Strings


torels
 Share

Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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). ^_^
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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]
Link to comment
Share on other sites

  • 3 weeks later...

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...