By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

Arrays printing errors

momotaro
100+
P: 357
pre: - array pos[3] containing the dots' position in the IP adress
- array IP[15] containing the IP string
post:- return 4 arrays xx[3], yy[3], zz[3], tt[3] containing the IP/4 digits

problem(remaining from 1st version): - unexpected result while checking using printf
example: - instead having "xx[3] = 123" am having 123:)
- third part of the IP 123 and some bizard char instead of 123
questions: - please help(if there is any other comment...)


CODE:

Expand|Select|Wrap|Line Numbers
  1. int i, j;
  2.  int xxtemp =0 , yytemp = 0, zztemp = 0, mmtemp = 0;
  3.  int y = 0, z = 0, m = 0;
  4.  char xx[3], yy[3], zz[3], mm[3];
  5.  char IP[] = {'1','2','3','.','1','2','3','.','1','2','3','.','1','2','3'};
  6.  int pos[] = {3,7,11};
  7.  for(i = 0; i < 4; i++)
  8.    {
  9.     if(i == 0)
  10.     {
  11.      for(j = i; j < pos[i]; j++)
  12.      {
  13.       if(isdigit(IP[j]))
  14.        xx[j] = IP[j];
  15.      }
  16.      printf("xxx part\n");
  17.         printf("%s\n", xx);
  18.     }
  19.     else if(i == 1)
  20.     {
  21.      for(j = pos[i-1]+1; j < pos[i]; j++)
  22.      {
  23.       if(isdigit(IP[j]))
  24.       {
  25.        yy[y] = IP[j];
  26.        y++;
  27.       }
  28.      }
  29.      printf("yyy part\n");
  30.      printf("%s\n", yy);
  31.     }
  32.     else if(i == 2)
  33.     {
  34.      for(j = pos[i-1]+1; j < pos[i]; j++)
  35.      {
  36.       if(isdigit(IP[j]))
  37.       {
  38.        zz[z] = IP[j];
  39.        z++;
  40.       }
  41.      }
  42.      printf("zzz part\n");
  43.      printf("%s\n", zz);
  44.     }
  45.     else if(i == 3)
  46.     {
  47.      for(j = pos[i-1]+1; j < 15; j++)
  48.      {
  49.       if(isdigit(IP[j]))
  50.       {
  51.        mm[m] = IP[j];
  52.        m++;
  53.       }
  54.      }
  55.      printf("mmm part\n");
  56.      printf("%s\n", mm);
  57.     }
  58.    }
Aug 7 '07 #1
Share this Question
Share on Google+
3 Replies


momotaro
100+
P: 357
here si a screenshot of the program output
Aug 7 '07 #2

ilikepython
Expert 100+
P: 844
pre: - array pos[3] containing the dots' position in the IP adress
- array IP[15] containing the IP string
post:- return 4 arrays xx[3], yy[3], zz[3], tt[3] containing the IP/4 digits

problem(remaining from 1st version): - unexpected result while checking using printf
example: - instead having "xx[3] = 123" am having 123:)
- third part of the IP 123 and some bizard char instead of 123
questions: - please help(if there is any other comment...)


CODE:

Expand|Select|Wrap|Line Numbers
  1. int i, j;
  2.  int xxtemp =0 , yytemp = 0, zztemp = 0, mmtemp = 0;
  3.  int y = 0, z = 0, m = 0;
  4.  char xx[3], yy[3], zz[3], mm[3];
  5.  char IP[] = {'1','2','3','.','1','2','3','.','1','2','3','.','1','2','3'};
  6.  
  7.  
There's an easier way to do this:
Expand|Select|Wrap|Line Numbers
  1. char xx[4], yy[4], zz[4], mm[4];   // include the \0
  2. char *Ips[4] = {xx, yy, zz, mm};
  3. char IP[] = "123.67.12.90";
  4.  
  5. int ind = 0;    // index in Ips
  6. int ind2 = 0;  // index in xx, yy ,zz, mm
  7.  
  8. for (int x = 0; x < strlen(IP); x++)
  9. {
  10.     if (IP[x] == '.')
  11.     {
  12.         Ips[ind][ind2] = '\0'   // null terminate
  13.         ind++;    // move on to next segment
  14.         ind2 = 0  // restart counter of segment to 0
  15.     }
  16.     else
  17.     {
  18.         Ips[ind][ind2] = IP[x];
  19.         ind2++;
  20.     }
  21. }
  22. Ips[ind][ind2] = '\0'  // terminate final segment because there is no period
  23.  
The segments of the Ip address will be stored in Ips.
Aug 7 '07 #3

momotaro
100+
P: 357
thank you it's working with some small modifications :)
Aug 7 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.