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

 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 int i, j;  int xxtemp =0 , yytemp = 0, zztemp = 0, mmtemp = 0;  int y = 0, z = 0, m = 0;  char xx[3], yy[3], zz[3], mm[3];  char IP[] = {'1','2','3','.','1','2','3','.','1','2','3','.','1','2','3'};  int pos[] = {3,7,11};  for(i = 0; i < 4; i++)    {     if(i == 0)     {      for(j = i; j < pos[i]; j++)      {       if(isdigit(IP[j]))        xx[j] = IP[j];      }      printf("xxx part\n");         printf("%s\n", xx);     }     else if(i == 1)     {      for(j = pos[i-1]+1; j < pos[i]; j++)      {       if(isdigit(IP[j]))       {        yy[y] = IP[j];        y++;       }      }      printf("yyy part\n");      printf("%s\n", yy);     }     else if(i == 2)     {      for(j = pos[i-1]+1; j < pos[i]; j++)      {       if(isdigit(IP[j]))       {        zz[z] = IP[j];        z++;       }      }      printf("zzz part\n");      printf("%s\n", zz);     }     else if(i == 3)     {      for(j = pos[i-1]+1; j < 15; j++)      {       if(isdigit(IP[j]))       {        mm[m] = IP[j];        m++;       }      }      printf("mmm part\n");      printf("%s\n", mm);     }    } Aug 7 '07 #1
Share this Question
3 Replies

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

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

 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.