Preamble: I suck at C and I'm proficient in Pascal.
While debugging a piece of C I got into a serious case of pointer mangling.
So, I asked ChatGPT to prepare a table comparing the two.
"please prepare a table comparing pointer data types and pointer
operands between FreePascal and C. Use FreePascal as the starting
point."
Resulted in a nicely prepared table and I quickly debugged my program
(for slow values of quickly).
But that's not it...
Then, on a whim: I asked: "
prepare a comparison of pascal record to C struc where the pascal record
has elements: a, b: word; c:array [1..10] of real; and next which is a pointer to the record type.
And it did. And it made an error. So I said to it:
"I believe you need to first have a pointer to the MyRecord such as: MyRecordPtr = ^MyRecord before the record and next should be of type MyRecordPtr."
ChatGPT replied:
"You are absolutely correct, and I apologize for the oversight. In both Pascal and C, when you have a pointer to a record or struct within the
same type definition, you should declare a type for that pointer first. Here's the corrected comparison:"
and put out the correct form (which is - for the case requested):
type
MyRecordPtr = ^MyRecord; // Declare a pointer type
MyRecord = record
a, b: Word;
c: array[1..10] of Real;
next: MyRecordPtr; // Use the pointer type
end;
and for C:
struct MyStruct {
unsigned short a;
unsigned short b;
float c[10];
struct MyStruct* next;
};
(Nasty C letting programmers declare new types and vars on the fly ...
sigh).
On 15/10/2023 20:51, Alan Browne wrote:
and put out the correct form (which is - for the case requested):Hmmm.
type
MyRecordPtr = ^MyRecord; // Declare a pointer type
MyRecord = record
a, b: Word;
c: array[1..10] of Real;
next: MyRecordPtr; // Use the pointer type
end;
and for C:
struct MyStruct {
unsigned short a;
unsigned short b;
float c[10];
struct MyStruct* next;
};
(Nasty C letting programmers declare new types and vars on the fly ...
sigh).
Those declarations do not represent the same types.
[1..10] is not the same as [10].
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (0 / 16) |
Uptime: | 158:10:03 |
Calls: | 10,384 |
Calls today: | 1 |
Files: | 14,056 |
Messages: | 6,416,482 |