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].
On 2023-10-15 20:56, moi wrote:https://chat.openai.com/g/g-2IqDEDp7c-essay-typer https://chat.openai.com/g/g-6sIiosQhh-write-my-essay-bot https://chat.openai.com/g/g-ldMROHWp6-gpt-paraphrasing-tool https://chat.openai.com/g/g-2IqDEDp7c-essay-typer https://chat.openai.com/g/g-wznfN7mwL-chatgpt-essay-writer https://chat.openai.com/g/g-BKo6liIEe-ai-essay-writer-by-eduwriter-ai https://www.bulbapp.com/u/ai-essay-writer
On 15/10/2023 20:51, Alan Browne wrote:
For Pascal "Real" is platform dependent and can be 32 or 64 bits.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].In C it would be 0 based, but in both you get a 10 element array of real numbers.
--
“Markets can remain irrational longer than your can remain solvent.”
- John Maynard Keynes.
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 919 |
Nodes: | 10 (1 / 9) |
Uptime: | 54:03:29 |
Calls: | 12,183 |
Calls today: | 3 |
Files: | 186,524 |
Messages: | 2,236,310 |