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);
}

Recommended Answers

All 6 Replies

Maybe this will help int *remain[] = {remain1,remain2,remain3,remain4};

Eternal49> Ok, I dont really know how to explain what im trying to do.
Take a look at this snippet. Pay attention to the introduction. It might help you understand what you are trying to do.

Concerning the errors.
> sub.c:46: error: subscripted value is neither array nor pointer

Take a look at line 46 of your code which is named sub.c

remain[j][i] = oct % 2;

it looks like some sort of array, but remain is an int according to line 37 int remain; See if you can figure out now this error. It is the same.

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

These are just a warning. It is telling you: Hey! you declared all these variables, but none are ever used in your code.

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’

Thanks Ancient Dragon, after using what you suggested it fixed my problem, and I made a few other changes to fix some new errors and warnings. Now the code compiles with no errors or warnings but after I enter a subnet mask i get a segmentation fault.

Any Ideas?

#include <stdio.h>
#include <stdlib.h>

void dec2bin(int ipray[], char bin1, char bin2, char bin3, char bin4);

int main()
{
  char str[16];
  int ipray[4];
  char bin1 = 0;
  char bin2 = 0;
  char bin3 = 0;
  char bin4 = 0;
  
  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[], char bin1, char bin2, char bin3, char 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 * remain[] = {remain1,remain2,remain3,remain4};
  char * bin[8];
  
  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);
}
int remain1[8];
  int remain2[8];
  int remain3[8];
  int remain4[8];
  int * remain[] = {remain1,remain2,remain3,remain4};

Why?

Why not a simple int remain[4][8]; ??

Ok, I ended up just doing it a different way, now it works but I am having an issue trying to combine an array of 8 numbers into one int, if I could just figure this out I would be good.

Something like sprintf for integers.

Here is my current code. All the random printf statements are for testing. I attempted to do bit shifting and "anding" but its not working out to well for me. ( See line 44)

#include <stdio.h>
#include <stdlib.h>

int dec2bin(int ipray);

int main()
{
  char str[16];
  int ipray[4];
  int bin[4];
  int i = 0;
  
  printf("Enter a Netmask :");
  scanf("%s", str);
 
  sscanf(str, "%d.%d.%d.%d", &ipray[0], &ipray[1], &ipray[2], &ipray[3]);
  
  do
  {
    bin[i] = dec2bin(ipray[i]);
    i++;
  } while ( i < 4 );
  
  printf("%d\n%d\n%d\n%d\n", bin[0],bin[1],bin[2],bin[3]);
  
 return 0;
  
}

int dec2bin(int ipray)
{
  int remain[8];
  int i = 0;
  
  printf("%d--\n", ipray);
  
  do
  {
    remain[i] = ipray % 2;
    ipray = ipray / 2;
    i++;
  } while ( i < 8 );
  
  int bin = {(remain[7]<<31) | (remain[6]<<30) | (remain[5]<<29) | (remain[4]<<28) | (remain[3]<<27) | (remain[2]<<26) | (remain[1]<<25) | (remain[0]<<24)};
  printf("%d--\n", bin); 
  return bin;
}

Maybe something of this sort

int i=0;
long int bin=0;

while(i<8)
{
   bin= bin+ rem[i]*(pow(10,i));
   i++; 
}
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.