Ok, I dont really know how to explain what im trying to do. So it might be easier to just look at my code. But basically im getting the following error messages when I try to compile the code, I know what error messages mean, I just dont know how to do what I want to do. Im trying to store data in certain variable arrays, but the way im tryin to specify certain variable arrays is like a 2d array but thats not what I want.
Im not even sure if the code will work because I cant compile it to test it, but basically I want to take an array ( ipray ) and convert each numerical value into binary, then store each 8 bit binary number into a variable. For example.
If ipray[4] equalled the following:
ipray[0] = 255
ipray[1] = 255
ipray[2] = 255
ipray[3] = 0
Then I would want:
bin1 = 11111111
bin2 = 11111111
bin3 = 11111111
bin4 = 00000000
Any Ideas?
nkowalski@nkowalski-desktop:~/Desktop$ gcc -Wall sub.c -o test2
sub.c: In function ‘dec2bin’:
sub.c:46: error: subscripted value is neither array nor pointer
sub.c:57: error: subscripted value is neither array nor pointer
sub.c:57: error: subscripted value is neither array nor pointer
sub.c:57: error: subscripted value is neither array nor pointer
sub.c:57: error: subscripted value is neither array nor pointer
sub.c:57: error: subscripted value is neither array nor pointer
sub.c:57: error: subscripted value is neither array nor pointer
sub.c:57: error: subscripted value is neither array nor pointer
sub.c:57: error: subscripted value is neither array nor pointer
sub.c:57: error: subscripted value is neither array nor pointer
sub.c:31: warning: unused variable ‘remain4’
sub.c:30: warning: unused variable ‘remain3’
sub.c:29: warning: unused variable ‘remain2’
sub.c:28: warning: unused variable ‘remain1’
#include <stdio.h>
#include <stdlib.h>
void dec2bin(int ipray[], int bin1, int bin2, int bin3, int bin4);
int main()
{
char str[16];
int ipray[4];
int bin1;
int bin2;
int bin3;
int bin4;
printf("Enter a Netmask :");
scanf("%s", str);
sscanf(str, "%d.%d.%d.%d", &ipray[0], &ipray[1], &ipray[2], &ipray[3]);
dec2bin(ipray, bin1, bin2, bin3, bin4);
return 0;
}
void dec2bin(int ipray[], int bin1, int bin2, int bin3, int bin4)
{
int remain1[8];
int remain2[8];
int remain3[8];
int remain4[8];
int i = 0;
int e = 0;
int j = 1;
int k = 1;
int bin;
int remain;
for (e = 0; e < 5; e++)
{
for (j = 1; j < 5; j++)
{
do
{
int oct = ipray[e];
remain[j][i] = oct % 2;
oct = oct / 2;
i++;
} while ( i < 8 );
}
}
for (k = 1; k < 5; k++)
{
for (j = 1; j < 5; j++)
{
sprintf(bin[k], "%d%d%d%d%d%d%d%d", remain[j][7],remain[j][6],remain[j][5],remain[j][4],remain[j][3],remain[j][2],remain[j][1],remain[j][0]);
}
}
printf("%d\n%d\n%d\n%d\n", bin1, bin2, bin3, bin4);
}