Thread: c seems over-exact

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    22

    Angry c seems over-exact

    Hello,

    I am new to C, and after plenty failstarts with tutorials i try to addept a routine for my real hobby ZX Spectrum and Z80 source.
    https://www.worldofspectrum.org/foru...Comment_899912

    how comes that adding strings is so complex and restricted?
    a chr$ is a chr$ and all you need is memory space.

    and writing an INT as an CHAR is a strange procedure.
    Even while all numbers a printed on screen as chr$ in an very automated way it seems very difficult to do so in a routine.
    FAQ > Convert an int to a string (C++) - Cprogramming.com

    i am probably stupid to expect a simple notation like
    number[] = string( int num);
    to give just ANY string of ANY INT number.
    its al so indirect and circumstancial.

    is above FAQ solution realy the expected way to solve my "problem" ???

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    I guess the first place to start is sprintf(), which is the swiss army knife for string building.

    > is above FAQ solution realy the expected way to solve my "problem" ???
    It's a fairly decent way for C++.
    But you posted in the C forum.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    22
    Ah, i use C++ in C , thanks for the reminder
    I will check sprintf()

    there are severall differnt print commands already, i will find them
    tnx

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > Ah, i use C++ in C , thanks for the reminder
    That's just C++ then.

    It's like this:
    C is one lane of traffic.
    C++ is another lane of traffic.
    C/C++ is the median line between them; spend too long there and you're gonna get hit by something you didn't see coming.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2017
    Posts
    22
    Quote Originally Posted by Salem View Post
    > Ah, i use C++ in C , thanks for the reminder
    That's just C++ then.

    It's like this:
    C is one lane of traffic.
    C++ is another lane of traffic.
    C/C++ is the median line between them; spend too long there and you're gonna get hit by something you didn't see coming.
    Like an "INT used as pointer error" makeing the value the memory adres. got that last days severall times

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Trying to learn C by making modifications to a large program is going to be a frustrating experience for you.

    Prototype your ideas in a completely separate program.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    // if you ever see <?> printed, you did something wrong with your decoding
    // it's better than seeing garbage or a crash.
    void test ( const char *data ) {
      const char *type="<?>" , *type0="<?>" , *type2="<?>" , *type3="<?>" ;
      unsigned char name[11] = { 0 }, zxarray[2] = { 0 };
      int offset = 0,  arr1 = 0, arr2 = 0, arr3 = 0 ;
      unsigned int data_adress = 0, zxlength = 0 ;
    
    	if (data[0] == 0x00) {
                     type0 = "headerblock";
                        }
                else {
                     type0 = "data__block";
                      }
    	
        /* Flag byte is 0x00 for headers */
        //if( data[0] != 0x00 ) goto normal;
    type3="" ;
        
        /* dat[0] is actualy zxdata[-1] */
    data_adress =   256 * data[14+1] + data[13+1] ;  /* 16bit value CODE startadres AND basic startadres 'LINE xxxxx' */    
    zxlength = 256 * data[12+1] + data[11+1] ;
    zxarray[1]= data[14+1];
        arr1 = zxarray[1]/32 ;
        arr2 = arr1 * 32 ;
        arr3 = zxarray[1]-arr2 ;
    zxarray[2] = 64+arr3 ; 
    
    
      switch( data[1] ) {
        case 0x00: type = "Program"; 
                   if ( data_adress < 10000 ) {
    		    type2 = " LINE " ;         /* line is OR bigger then 9999 OR smaller then 1000 */
                        type3 = data_adress ;      /* only given if AUTOSTART is enabled by value data_adress */ 
    	      }
                   break;
    	       
        case 0x01: type = "Number array"    ; type2 = " DATA "  ; char *strcat (const char *type2, char *zxarray[2] );  type3 = "() " ; break;
        case 0x02: type = "Character array" ; type2 = " DATA "  ; char *strcat (const char *type2, char *zxarray[2] );  type3 = "$()" ; break;
        case 0x03: type = "Bytes";
                   type2 = " CODE ";
                   if (data_adress == 16384 && zxlength == 6912 ) type2 = " SCREEN$  ";
    	       type3 = data_adress;
    	       break;
        }
        
        printf("%s |  %s: \"%s\" %s %s , %d" ,type0, type, "noname", type2, type3, zxlength );
    
    }
    
    int main ( ) {
      // add as many variations of these as you feel you need
      const char d1[] = {
        0x00, 0x00, 0x00, 0x00, 0x00,  // 0 to 4
        0x00, 0x00, 0x00, 0x00, 0x00,  // 5 to 9
        0x00, 0x00, 0x00, 0x00, 0x00,  // 10 to 15 - these seem very interesting to you
      };
      test(d1);
      return 0;
    }
    Focus on getting this to compile and run as a separate standalone console program.

    Some thoughts:
    zxarray is declared with 2 elements, yet you access [1] and [2] indices.
    Arrays start at [0], not [1].

    Also, you seem to want to use zxarray as a string (either printing, or with strcat).
    But you need to make sure it has a \0 stored in the array.

    So if you want two printable characters at [0] and [1], then [2] must be a \0 character.
    This implies that you need at least char zxarray[3] to begin with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2017
    Posts
    22
    Quote Originally Posted by Salem View Post
    Trying to learn C by making modifications to a large program is going to be a frustrating experience for you.

    Prototype your ideas in a completely separate program.
    Thats very true, except that i need a purpose to start with C.
    And with this adaptation i have a learning curve
    With 'Fuse' i have the extra that i know what the program wants.

    But it would be much better to re-write one of my basic progs and learn by porting to C.
    What did i learn so far?
    partly why it never worked before for me.
    the "int" to "char" is in basic all automaticly aranged, in ZX in the background with the preserved "variable-memory-area" which has a kind off "auto" malloc for variable
    C is older then Basic, in C you need to define the memory space PER variable and PER variable calculation. every thing must be preset before any compilation and runtime

    the idear of a pointer is not very difficult, its like a systemvariable that points-out where the (standard)font is eg.
    yet it get complex when C insist to transfer the pointer ADRES instead of that vaiable.
    it has not much use to go over an "trail on error" path.
    For the moment i will asume that someone from the fuse-group will see the adaptations and work it out to a correct working "tape-browser", that would be very nice.
    As you suggested before, i go read about "sprintf()" and probably use it in my "endian-test" which needs more attention anyway
    https://www.worldofspectrum.org/foru...cussion/28991/

  8. #8
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by CBorn View Post
    C is older then Basic, in C you need to define the memory space PER variable and PER variable calculation. every thing must be preset before any compilation and runtime
    BASIC was originally designed in 1964. C was developed between 1969 and 1973. The differences you are seeing is not about age but about power at the cost of complexity. C blows BASIC out of the water but is harder to use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. atof is not exact
    By wakeup in forum C Programming
    Replies: 3
    Last Post: 04-02-2008, 09:46 AM
  2. Exact binary in C++
    By vddking2 in forum C++ Programming
    Replies: 6
    Last Post: 04-23-2007, 05:43 AM
  3. Exact Match
    By pritin in forum C++ Programming
    Replies: 2
    Last Post: 03-26-2007, 08:50 AM
  4. getting exact input
    By chrismiceli in forum C Programming
    Replies: 5
    Last Post: 05-06-2004, 10:48 AM
  5. 1 question (well... 2 to be exact)
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-01-2003, 03:18 PM

Tags for this Thread