Problem with binary strings

P

Pete

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
 
B

Barry Schwarz

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>>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top