Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

copying elements to an array of pointers..

compengcompeng Member Posts: 7
i'd like to copy a char array to an array of pointers.. the code should look like this:

#include
#include
#include
#include
#define max 5

int main(){
char *array[max];
char string[max];
cout<<"enter string :";
cin.getline(string,max);

strncpy(array[0],string,sizeof(string)/sizeof(char));


printf("%s",array[0]);

return 0;
}

i can't get it working and i used printf because i can't see the array[0] with cout... anyone who has an idea??? anyone who can help??
kind regards...

Comments

  • KayDeeKayDee Member Posts: 111
    : i'd like to copy a char array to an array of pointers.. the code should look like this:
    :
    : #include
    : #include
    : #include
    : #include
    : #define max 5
    :
    : int main(){
    : char *array[max];
    : char string[max];
    : cout<<"enter string :";
    : cin.getline(string,max);
    :
    : strncpy(array[0],string,sizeof(string)/sizeof(char));
    :
    :
    : printf("%s",array[0]);
    :
    : return 0;
    : }
    :
    : i can't get it working and i used printf because i can't see the array[0] with cout... anyone who has an idea??? anyone who can help??
    : kind regards...
    :
    As for your cout "problem", it probably goes for cin too...I can't see any "using namespace std;"...might be it don't you think?


  • compengcompeng Member Posts: 7
    : : i'd like to copy a char array to an array of pointers.. the code should look like this:
    : :
    : : #include
    : : #include
    : : #include
    : : #include
    : : #define max 5
    : :
    : : int main(){
    : : char *array[max];
    : : char string[max];
    : : cout<<"enter string :";
    : : cin.getline(string,max);
    : :
    : : strncpy(array[0],string,sizeof(string)/sizeof(char));
    : :
    : :
    : : printf("%s",array[0]);
    : :
    : : return 0;
    : : }
    : :
    : : i can't get it working and i used printf because i can't see the array[0] with cout... anyone who has an idea??? anyone who can help??
    : : kind regards...
    : :
    : As for your cout "problem", it probably goes for cin too...I can't see any "using namespace std;"...might be it don't you think?
    :
    :
    : thanx a lot:))

  • Geeth AsokanGeeth Asokan Member Posts: 127
    Hi,

    Instead of using [b]printf[/b], you should be using [b]cout[/b].

    [code]
    #include
    #include
    #include
    #include
    #define max 5

    int main()
    {
    char *array[max];
    char string[max];

    cout <<"enter string :";
    cin.getline(string,max);

    strncpy(array[0],string,sizeof(string)/sizeof(char));


    cout << array[0];

    return 0;
    }
    [/code]

    This may work. I haven't tried it out.


    : i'd like to copy a char array to an array of pointers.. the code should look like this:
    :
    : #include <iostream.h>
    : #include
    : #include
    : #include
    : #define max 5
    :
    : int main(){
    : char *array[max];
    : char string[max];
    : cout<<"enter string :";
    : cin.getline(string,max);
    :
    : strncpy(array[0],string,sizeof(string)/sizeof(char));
    :
    :
    : printf("%s",array[0]);
    :
    : return 0;
    : }
    :
    : i can't get it working and i used printf because i can't see the array[0] with cout... anyone who has an idea??? anyone who can help??
    : kind regards...
    :

  • KayDeeKayDee Member Posts: 111
    : : : i'd like to copy a char array to an array of pointers.. the code should look like this:
    : : :
    : : : #include
    : : : #include
    : : : #include
    : : : #include
    : : : #define max 5
    : : :
    : : : int main(){
    : : : char *array[max];
    : : : char string[max];
    : : : cout<<"enter string :";
    : : : cin.getline(string,max);
    : : :
    : : : strncpy(array[0],string,sizeof(string)/sizeof(char));
    : : :
    : : :
    : : : printf("%s",array[0]);
    : : :
    : : : return 0;
    : : : }
    : : :
    : : : i can't get it working and i used printf because i can't see the array[0] with cout... anyone who has an idea??? anyone who can help??
    : : : kind regards...
    : : :
    : : As for your cout "problem", it probably goes for cin too...I can't see any "using namespace std;"...might be it don't you think?
    : :
    : :
    : : thanx a lot:))
    :
    So it's working now?

  • compengcompeng Member Posts: 7
    : : : : i'd like to copy a char array to an array of pointers.. the code should look like this:
    : : : :
    : : : : #include
    : : : : #include
    : : : : #include
    : : : : #include
    : : : : #define max 5
    : : : :
    : : : : int main(){
    : : : : char *array[max];
    : : : : char string[max];
    : : : : cout<<"enter string :";
    : : : : cin.getline(string,max);
    : : : :
    : : : : strncpy(array[0],string,sizeof(string)/sizeof(char));
    : : : :
    : : : :
    : : : : printf("%s",array[0]);
    : : : :
    : : : : return 0;
    : : : : }
    : : : :
    : : : : i can't get it working and i used printf because i can't see the array[0] with cout... anyone who has an idea??? anyone who can help??
    : : : : kind regards...
    : : : :
    : : : As for your cout "problem", it probably goes for cin too...I can't see any "using namespace std;"...might be it don't you think?
    : : :
    : : :
    : : : thanx a lot:))
    : :
    : So it's working now?
    :
    :

  • compengcompeng Member Posts: 7
    : : : : i'd like to copy a char array to an array of pointers.. the code should look like this:
    : : : :
    : : : : #include
    : : : : #include
    : : : : #include
    : : : : #include
    : : : : #define max 5
    : : : :
    : : : : int main(){
    : : : : char *array[max];
    : : : : char string[max];
    : : : : cout<<"enter string :";
    : : : : cin.getline(string,max);
    : : : :
    : : : : strncpy(array[0],string,sizeof(string)/sizeof(char));
    : : : :
    : : : :
    : : : : printf("%s",array[0]);
    : : : :
    : : : : return 0;
    : : : : }
    : : : :
    : : : : i can't get it working and i used printf because i can't see the array[0] with cout... anyone who has an idea??? anyone who can help??
    : : : : kind regards...
    : : : :
    : : : As for your cout "problem", it probably goes for cin too...I can't see any "using namespace std;"...might be it don't you think?
    : : :
    : : :
    : : : thanx a lot:))
    : :
    : So it's working now?
    :
    : yes it is...

  • Justin BibJustin Bib USAMember Posts: 0

    __ { http://forcoder.org } free ebooks and video tutorials about // JavaScript, Scratch, Delphi, PHP, R, PL/SQL, Python, MATLAB, C++, Perl, Objective-C, C#, Visual Basic .NET, C, Ruby, Swift, Assembly, Visual Basic, Java, Go Kotlin, Ada, Dart, Julia, Transact-SQL, D, Scala, COBOL, Apex, F#, Lua, Prolog, Erlang, Fortran, Hack, ABAP, Awk, Clojure, VBScript, LabVIEW, Bash, Logo, Rust, FoxPro, Scheme, SAS, ML, Lisp, Alice, Crystal // _____

Sign In or Register to comment.