Problem with binary strings

Discussion in 'C Programming' started by Pete, Sep 6, 2004.

  1. Pete

    Pete Guest

    I'm trying to a very simple encryption key generator. I have hard coded a 10
    binary string into an array, I then want to permute that string using
    another array with element of type int for 1 to 10. Then I'm using this as
    an index to copy to an output array, but offcourse the integer 10 is reading
    the non-existent 11th element of, could someone help with a solution to my
    problem, I would prefer to get the binary string from keyboard but I had
    problems. below is the fragment I'm having problems with.

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

    main()
    {
    int string[10]={1,1,0,1,0,0,0,1,1,1};
    int P10[10]={3,5,2,7,4,10,1,9,8,6};
    /*int input[10];*/
    int P10_out[10];
    int i,index;

    /*printf("\nEnter a 10 Binary Sting: ");
    for(i=0; i<10; i++){
    scanf("%d", input);
    }*/
    for(i=0; i<10; i++) {
    index = P10;
    P10_out = string[index];
    printf("P10_out = %d\n", P10_out);
    }
    }

    As you can see I tyied to enter from the keyboard but the binary string
    converted to an int I think
    Pete, Sep 6, 2004
    #1
    1. Advertising

  2. On Mon, 6 Sep 2004 22:50:43 +1000, "Pete" <> wrote:

    >I'm trying to a very simple encryption key generator. I have hard coded a 10
    >binary string into an array, I then want to permute that string using
    >another array with element of type int for 1 to 10. Then I'm using this as
    >an index to copy to an output array, but offcourse the integer 10 is reading
    >the non-existent 11th element of, could someone help with a solution to my
    >problem, I would prefer to get the binary string from keyboard but I had
    >problems. below is the fragment I'm having problems with.
    >
    >#include <stdio.h>
    >#include <string.h>
    >
    >main()
    >{
    > int string[10]={1,1,0,1,0,0,0,1,1,1};
    > int P10[10]={3,5,2,7,4,10,1,9,8,6};
    > /*int input[10];*/
    > int P10_out[10];
    > int i,index;
    >
    > /*printf("\nEnter a 10 Binary Sting: ");
    > for(i=0; i<10; i++){
    > scanf("%d", input);


    You commented out the definition of input above. This code won't
    compile.

    > }*/
    > for(i=0; i<10; i++) {
    > index = P10;


    When i is 5, index is 10.
    > P10_out = string[index];


    string[10] does not exist. The indices for string run from 0 to 9.
    This invokes undefined behavior.

    > printf("P10_out = %d\n", P10_out);
    > }
    >}
    >
    >As you can see I tyied to enter from the keyboard but the binary string
    >converted to an int I think


    Until you show us your real code, we have no idea what you are talking
    about.


    <<Remove the del for email>>
    Barry Schwarz, Sep 6, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fangs
    Replies:
    3
    Views:
    9,763
    darshana
    Oct 26, 2008
  2. Pete

    Problem with binary strings

    Pete, Sep 7, 2004, in forum: C Programming
    Replies:
    2
    Views:
    341
    Barry Schwarz
    Sep 8, 2004
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    739
    Malcolm
    Jun 24, 2006
  4. ruffiano

    Are C++ strings binary strings?

    ruffiano, Nov 1, 2006, in forum: C++
    Replies:
    9
    Views:
    1,153
  5. Replies:
    2
    Views:
    943
Loading...

Share This Page