Jump to content

using a variable as array index

- - - - -

  • Please log in to reply
4 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi

Please have a look on the two codes below which differ slightly. CODE 1 is working fine but CODE 2 won't run. I remember I have been told not to use a variable as an array index. But I don't see any rationale for not using a variable the way I used in CODE 2. Please help me with it.


CODE 1

// practice_array_string.cpp

// this program demostrates arrays and c-strings


#include <iostream>

#include <cstdlib>


using namespace std;


int main()

{

    int v;

    [B]//char str[v];[/B]


    cout << "enter string length: "; cin >> v;

    [B]char str[v];[/B]

    cout << "enter string: "; cin >> str;


    cout << str << endl;


    system("pause");

    return 0;

}


CODE 2

// practice_array_string.cpp

// this program demostrates arrays and c-strings


#include <iostream>

#include <cstdlib>


using namespace std;


int main()

{

    int v;

    [B]char str[v];[/B]


    cout << "enter string length: "; cin >> v;

    

    cout << "enter string: "; cin >> str;


    cout << str << endl;


    system("pause");

    return 0;

}


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
It's because v hasn't yet been given a value. Here is how I would do it:
// practice_array_string.cpp
// this program demostrates arrays and c-strings

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    int v;
    char *str;

    cout << "enter string length: "; cin >> v;
    str = new char[v];
    cout << "enter string: "; cin >> str;

    cout << str << endl;

    system("pause");
    return 0;
}

Latinamne loqueris?

#3
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi

What does the keyword "new" signify? Please let me know. Thanks.


cout << "enter string length: "; cin >> v;

str = [COLOR="#FF0000"]new[/COLOR] char[v];

cout << "enter string: "; cin >> str;


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#4
Airplaneman19

Airplaneman19

    Newbie

  • Members
  • Pip
  • 7 posts
You can't normally declare an array with a non-const integer, but you can if you use the "new" operator. I think you have to match if with "delete str;" at the end (please correct me if i'm wrong).

#5
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
@Airplaneman19 yes you do, i forgot lol.
@jackson6612 it is for something called dynamic memory allocation. Basically, you first declare a pointer, then you use the 'new' operator to allocate space for the pointer. For example these two have equivalent function:

char *str = new[SIZE] char;


char str[SIZE];

You also have to 'delete' it at the end. The matching delete statement would be this:

delete[] str;


You can also apply it to regular variables (non-arrays) like this:

int *a = new int;

//Some code...

delete a;

Notice there are no brackets for the 'new' and 'delete' operators when you aren't using an array.
Latinamne loqueris?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users