Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Any C programmers wanna help convert code to convert between MIDI + CSV?
#31
@Jrace

Quote:"Hence the old joke that Pascal is a Read-Only language, and C is a Write-Only language.

As I was learning C, experimenting with things like the ternary conditional operator (? ... :...), I discovered just how easy it is to make an unreadable, unmaintainable mess (see https://en.wikipedia.org/wiki/Internatio...de_Contest for examples, all done just for fun!).  You can nest ternaries within ternaries, etc, which all feels clever & fun until you have to find deeply nested bugs."


I have taken a look at that webpage!
It is the fashinating dark side of C...
as you have understood I like Pascal side but under this point of view C side is a temptation!
Reply
#32
(08-29-2022, 06:55 PM)JRace Wrote:
(08-29-2022, 12:18 AM)TempodiBasic Wrote:
Quote:Personally, I'd convert EVERY line of C code to a Basic comment, then start opening it up, adding spaces and blank lines, dividing it into blocks and figuring out what each block does, and what each line within each block does.
Lol, it spends a lot of time!

It is methodical.  It is useful if you are going in cold, with no idea how the original program works.  How long it takes depends on the complexity of the original source.  If the source is organized into sections or blocks which can be isolated, then as you figure out how each section works you can rewrite that section or block in Basic.

I'm doing this to one of my old programs, translating from Lua to Basic.  Rather than rewrite from scratch and go through all the debugging and fine tuning needed, I'm just translating and improving my old program.  I'm not under any productivity quotas, so there is no hurry.


Wow this is hard but efficacy! 
I would have the energy to do...
Quote:[quote pid="5968" dateline="1661799332"]
Now that I've done the interesting part of solving the major problems, I just need to motivate myself to do the tedious part and finish the job.   Sad
here a suggestion (or 101 suggestions)
https://sourcesofinsight.com/101-ways-to...nd-others/
[/quote]
Reply
#33
Quote:Oh yeah, you can create multidimensional arrays in C, but a string literal such as "Hello, world!" is treated as a one dimensional array of characters, with a NULL (0) as the last element.

If you try to create a multidimensional string array in C, you are really only creating an array of pointers to strings.


You have a fundamental problem understanding how multidimensional arrays are stored in the computer.

Regardless of whether it is one, two or three-dimensional, it is stored linearly in the computer. But the numerical example shows that the individual values can only be accessed via <row> - column>. The same applies to character strings.

Character strings in C are basically fields of the "char" type. And regarding of a pointer is an unindexed array name a pointer to the first array element:
char x[10];
The following two statements are identical:
x
&x[0]

Example of a one- and two-dimensional string field:

Code: (Select All)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
    //One-dimensional array
    char namen[] = "Rio de Janeiro";
    char *zgr = namen;
    
    //Two-dimensional array
    char namenfeld[3][9] = {"Berlin", "Tokio", "Katmandu"};
    int feldnummer;
        
    printf("\n");
    while( *zgr )
    { printf("%c", *zgr++); }

    printf("\n\n");
    
for(int i = 0; i < 3; i++)
    {
        printf("%s ", namenfeld[i]);        
    }
    printf("\n");
    
    printf("\nFeldnummer des Namesn der angezeigt werden soll (0 bis 2): ");
    scanf("%d", &feldnummer);
    printf("[%d] ist die Adresse von: %s", feldnummer, namenfeld[feldnummer]);
    
    printf("\n\n");
    return(0);
}

[Image: Felder-im-Computer.jpg]
Reply
#34
(08-30-2022, 06:50 PM)Kernelpanic Wrote: You have a fundamental problem understanding how multidimensional arrays are stored in the computer.

Regardless of whether it is one, two or three-dimensional, it is stored linearly in the computer. But the numerical example shows that the individual values can only be accessed via <row> - column>. The same applies to character strings.

Character strings in C are basically fields of the "char" type. And regarding of a pointer is an unindexed array name a pointer to the first array element:
char x[10];
The following two statements are identical:
x
&x[0]

I think it's worth mentioning that it's easy to get it confused because an array of pointers and a multidimensional array can both be accessed by doing `[a][b]`. But they are fundamentally different as you've noted, multidimensional arrays are one block of memory and the index gets calculated via the size of the array, there is only one memory access going on when you do `[a][b]`. Where-as if you use `[a][b]` on a `char *strs[10]` then `[a]` and `[b]` do separate memory accesses, once in the array of pointers and another in the array of char's.

I also think the difference gets a bit more obvious once you start to see pointers to arrays. An array of string pointers like `char *strs[10]` can be assigned and used as a `char **`, but a `char a[3][9]` cannot be treated as one. Instead, it can be treated as a `char (*x)[9]`, a pointer to an array of 9 chars, with `char a[3][9]` then being thought of as an array of arrays of 9 chars, all together in one thing.
Reply
#35
Yes, it's not easy in C! I had to look at my books again - it's been a long time since I worked with all that stuff.

The pointers have it all!  When I started over 25 years ago, I blew up my system twice with pointer arithmetic and pointer to pointer.
Reply
#36
(08-30-2022, 07:29 PM)Kernelpanic Wrote: Yes, it's not easy in C! I had to look at my books again - it's been a long time since I worked with all that stuff.

The pointers have it all!  When I started over 25 years ago, I blew up my system twice with pointer arithmetic and pointer to pointer.

Agreed, I never really understood multidimensional arrays until I tried to assign one to a `char **` and it wouldn't let me Big Grin And if you cast it to make the error go away you're going to have a bad time...
Reply
#37
(08-30-2022, 06:50 PM)Kernelpanic Wrote:
Quote:Oh yeah, you can create multidimensional arrays in C, but a string literal such as "Hello, world!" is treated as a one dimensional array of characters, with a NULL (0) as the last element.

If you try to create a multidimensional string array in C, you are really only creating an array of pointers to strings.


You have a fundamental problem understanding how multidimensional arrays are stored in the computer.

Regardless of whether it is one, two or three-dimensional, it is stored linearly in the computer. But the numerical example shows that the individual values can only be accessed via <row> - column>. The same applies to character strings.

Character strings in C are basically fields of the "char" type. And regarding of a pointer is an unindexed array name a pointer to the first array element:
char x[10];
The following two statements are identical:
x
&x[0]

Example of a one- and two-dimensional string field:

Code: (Select All)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
    //One-dimensional array
    char namen[] = "Rio de Janeiro";
    char *zgr = namen;
    
    //Two-dimensional array
    char namenfeld[3][9] = {"Berlin", "Tokio", "Katmandu"};
    int feldnummer;
        
    printf("\n");
    while( *zgr )
    { printf("%c", *zgr++); }

    printf("\n\n");
    
for(int i = 0; i < 3; i++)
    {
        printf("%s ", namenfeld[i]);        
    }
    printf("\n");
    
    printf("\nFeldnummer des Namesn der angezeigt werden soll (0 bis 2): ");
    scanf("%d", &feldnummer);
    printf("[%d] ist die Adresse von: %s", feldnummer, namenfeld[feldnummer]);
    
    printf("\n\n");
    return(0);
}

[Image: Felder-im-Computer.jpg]

There IS a misunderstanding here, but don't be so self-assured when assigning blame.  Maybe I didn't explain things thoroughly enough, or maybe there's a language barrier in the way.

I didn't respond to your previous post about multi-dimensional arrays because you were talking about declared arrays, not quoted strings.  Not wanting to post a correction, and having no need to show off, I let your post slide unanswered.

I am describing apples, you are describing oranges.  I was talking about strings, which in C are arrays of characters all lined up in neat little rows in memory.  Notice that I wrote "rows": a row has only ONE dimension.  Each quoted string is a single row of characters, all by itself.  It may have many columns, but it only has one row.  I was NOT talking about arrays of strings, which are a completely different things.

A "string" - with double quotes around it like "this" - is a simple character array, with ONE DIMENSION.  I am not talking about an array of strings, which is an array of pointers to strings, with the strings themselves being stored elsewhere.

This is an easy point of confusion for people who don't regularly program in C or assembly, which I have been doing for, oh, about 30 years now in C, and 40+ years in assembly.

Yes, you can create multi-dimensional arrays of characters.  You can even NULL-terminate each row to create a block of C-style strings accessible through pointers.  You can even create a pointer to a multi-dimensional array and load the address of your quoted string literal into it, to access it through the pointer.  Those are NOT what I have been talking about.

My subject was string literals.  When a C compiler encounters a string literal like this: "this is a character string", the compiler stores that string with other program data and ensures that the string will be loaded into memory when the executable is run.  The compiler substitutes a pointer - the address of the string - into to the program at the point where the string was used.  That pointer is a character pointer (char *), and can be used as such.  To get technical, source which reads letter="abcde"[3] will be translated by the compiler to letter=*(<address_of_abcde> + (3 * sizeof (char))).

C strings have only ONE row: a string in C is a one dimensional array of characters, real characters all side-by-side in memory, NOT an array pointers to strings.  Just as each quoted string in C source code stands alone, so do each of those compiler-generated character arrays stand alone.

THAT is what I was talking about.

A string literal's address can be used just like the address a ONE DIMENSIONAL array.  "string"[i] is perfectly valid C, while "string"[i][j] will generate a compile-time error because it makes no sense.


Quote:Regardless of whether it is one, two or three-dimensional, it is stored linearly in the computer. But the numerical example shows that the individual values can only be accessed via <row> - column>. The same applies to character strings.

As I've tried to explain, quoted character strings can only be accessed via <column>.  (Unless you want to play with pointers, but that was not what I was talking about.)

If you want to experiment, arrays in C can be accessed however you want, if you understand how the array data is laid out and are willing to play with pointers.  You can get experience using "#define"s in the preprocessor to make life a bit easier.
Reply
#38
This looks like an exciting and fun project. I just wish I knew more about C in order to help out. Instead, I'll just watch the progress from the sidelines Smile
Tread on those who tread on you

Reply
#39
Quote:As I've tried to explain, quoted character strings can only be accessed via <column>.


That's just wrong! The names in the example are accessed through the row.

All you need to do is run the slightly expanded program. By specifying the column one can only  access individual letters .

Code: (Select All)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
    //One-dimensional array
    char namen[] = "Rio de Janeiro";
    char *zgr = namen;
    
    //Two-dimensional array
    char namenfeld[3][9] = {"Berlin", "Tokio", "Katmandu"};
    int feldnummer, zeile, spalte;
        
    printf("\n");
    while( *zgr )
    { printf("%c", *zgr++); }

    printf("\n\n");
    
    for(int i = 0; i < 3; i++)
    {
        printf("%s ", namenfeld[i]);        
    }
    printf("\n");
    
    printf("\nFeldnummer des Namesn der angezeigt werden soll (0 bis 2): ");
    scanf("%d", &feldnummer);
    printf("[%d] ist die Adresse von: %s", feldnummer, namenfeld[feldnummer]);
    
    printf("\n\nWaehle Wort und Buchstaben die angezeigt werden sollen - Zeile<>Spalte: ");
    scanf("%d %d", &zeile, &spalte);
    printf("\nBuchstabe an Position [%d][%d]: %c", zeile, spalte, namenfeld[zeile][spalte]);
    printf("\n\nBuchstabe an Position [0][5]: %c", namenfeld[0][5]);
    
    printf("\n\n");
    return(0);
}

[Image: Stringfelder.jpg]
Reply
#40
(08-31-2022, 05:10 PM)Kernelpanic Wrote:
Quote:As I've tried to explain, quoted character strings can only be accessed via <column>.


That's just wrong! The names in the example are accessed through the row.

Nothing wrong about it, except that it could have been written better.  The line you attacked should have read:
Quote:quoted character strings can be directly accessed only via <column>.


Still apples vs oranges.  I see where your confusion comes from.  I freely admit to being a lousy teacher.  I have neither the communication skills nor the patience, so my explanations probably weren't thorough enough.

While your example creates character arrays, you are using string literals as array initializers.

The subject was about how strings were indexed directly by an existing program, NOT about how someone else might use strings in a new program.

I was explaining how string literals, which the compiler treats as character arrays, may be accessed DIRECTLY as arrays.  The subject was NOT about accessing arrays declared by the programmer.  While string literals can be used to initialize programmer-defined arrays, that was not the subject.

My posts have not been about alternative ways of accessing string data, because that would confuse things even more.  Alternatives would be off-topic.  My posts have been STRICTLY about how one particular program was designed to access string data.  Period.


This was the subject:
c="this"[3];
/* Nothing wrong with this */

not this:
char s[]="this";
...
c=s[3];
/* This is not the subject under discussion */




( @Spriggsy : I envy you.  Enjoy the show!)
Reply




Users browsing this thread: 1 Guest(s)