Hi all,

i made some java few time ago and i'm now studying C++
here is a simple program i've been working on to learn about it but the fact is for some days now i can't figure why it doesn't work...

in fact i just want to create an array of strings, noms[10]

my problem is that the first name will be stored all right in " noms[0] " , but then if i add a second name, it will be stored both in " noms[0] " and " noms[1] " ... and again same problem with a third name in noms[0] noms[1] and noms[2] ...

perhaps it's a real simple one but i don't really understand why data will go on " noms[0] " if i'm considering the address of " noms[1] "

thank you
vincent

#include <iostream>
#include <stdio.h>
#include <iostream.h>
#include <string.h>
#include <istream>


using namespace std;

int j=0;
char *noms[10];
char **ptr;

void add() {

        char name[20];

        cout << "Enter a name" << "\n";
        cin >> name;

        *ptr = name;

        cout << "voici noms[" << j << "] :" << noms[j] << "\n";
        cout << "voici noms[0]" << noms[0] << "\n";
        return;
}




int main()
{
    int x;
    ptr = &noms[0];

    while (j<11) {

       cout << " Enter your choice :" << "\n";
       cin >> x;

       if ( x ==1 ) {
            add();
            j++;
            ptr++;
       }
    }
}
Member Avatar for jencas

1. don't mix C and C++ this way
2. use std::string instead of char[]
3. try to avoid including .h files, the c++ header files have no trailing .h
4. to copy char[], use strcpy()
5. use code tags for better reading

Please use code tags, [CODE=cplusplus] [/code].

Firstly, is this C or C++ because it looks like a strange confusion between the two. The only header you should be using in this program is #include <iostream> not with a .h or anything else funky.

I'd suggest you go back over the very basics of C++ before attempting to use pointers etc...since you are using global variables..which is not good.

And you obviously don't understand pointers...

Chris

well perhaps indeed my language is not really a C++ one; to be honest i only used what i found on forum/books/ tutorials. sorry these are not complient with your way of coding
concerning package i know but i don't really think it's the core of my problem here
concerning pointers, coming from java indeed i have some lack. now no where i found explanations about this precise point, thus i just wanted to know if someone could help me . i know it might be kinda boring for u guys to see always same mistakes form newbies, now i spent a week looking on forum and on books a solution, i found not.

Basically what this code is doing is that --

In first step you are putting some value in name and norms[0] pointing to name.

In second step you changing value in name and norms[1] now also points to name.
Since both norms[0] and norms[1] points to name new value is reflected by both of them.

Suggestion: You should have array of names so norms[0], norms[1] can point to different names.

oh ok i see ... yeah, of course .
thx a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.