473,408 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Arrays printing errors

momotaro
357 100+
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
3 1584
momotaro
357 100+
here si a screenshot of the program output
Aug 7 '07 #2
ilikepython
844 Expert 512MB
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
357 100+
thank you it's working with some small modifications :)
Aug 7 '07 #4

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

Similar topics

35
by: Troll | last post by:
Hi, I need to write a script which reads some data and reports the findings. Just to give you an idea the structure is similar to the following. Data input example: HEADING 1 **********...
9
by: Jody Gelowitz | last post by:
I am trying to find the definition of "Safe Printing" and cannot find out exactly what this entitles. The reason is that I am trying to print contents from a single textbox to no avail using the...
27
by: jacob navia | last post by:
Has anyone here any information about how arrays can be formatted with printf? I mean something besides the usual formatting of each element in a loop. I remember that Trio printf had some...
1
by: Jonathan DeCarlo | last post by:
I have a situation where I need to override a method in managed C++ that was originally defined in C#. The method takes a parameter of an array of arrays of doubles. Here is a simple examples...
3
by: John Peterson | last post by:
Hello all! I'm at my wits end trying to search for what I assumed to be a relatively straightforward task. I have a Web application written in C#, and I have a button on the form that I want to...
1
by: eskildb | last post by:
First, please be gently. I am fairly new to the programming world (1.5 years with some expermentation prior to). I have been working on a project that has to print HTML pages with graphics in a...
1
by: eskildb | last post by:
First, please be gently. I am fairly new to the programming world (1.5 years with some expermentation prior to). I have been working on a project that has to print HTML pages with graphics in a...
6
by: Siv | last post by:
Hi, I am getting into printing with VB.NET 2005 and want to implement the usual capability that a user can select a selection of pages. I have a report that is generated by my application that if...
3
by: =?Utf-8?B?TWFyazYyNA==?= | last post by:
I have a web method that renders a SQL Reporting Services report and prints it to a network printer using the System.Drawing and System.Drawing.Printing namespaces in .Net 2.0. This method has...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.