Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AndrewAMD, VoroneTZ), 831 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
lite-C bug #467822
09/03/17 13:05
09/03/17 13:05
Joined: Aug 2017
Posts: 58
P
pascalx Offline OP
Junior Member
pascalx  Offline OP
Junior Member
P

Joined: Aug 2017
Posts: 58
Guys. Why?
http://www.zorro-trader.com/manual/en/apointer.htm

Code:
struct S
{
	int* P;
};

void main()
{
	struct S s;
	s.P = malloc(sizeof(int));
	*(s.P) = 1234; // Ok
	s.P[0] = 1234; // Error: subscript requires array or pointer type
	int* p = s.P;
	p[0] = 2345; // Ok
	printf("%d", *(s.P));
	free(s.P);
}


Re: lite-C bug [Re: pascalx] #467823
09/03/17 13:30
09/03/17 13:30
Joined: Feb 2017
Posts: 1,718
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,718
Chicago
This doesn't work in normal C either. (Whoops, yes it does.)

Code:
s.P = malloc(sizeof(int));



(C++) Visual Studio says...
"a value of type "void *" cannot be assigned to an entity of type "int *"

Last edited by AndrewAMD; 09/04/17 16:41.
Re: lite-C bug [Re: AndrewAMD] #467824
09/03/17 13:57
09/03/17 13:57
Joined: Aug 2017
Posts: 58
P
pascalx Offline OP
Junior Member
pascalx  Offline OP
Junior Member
P

Joined: Aug 2017
Posts: 58
Originally Posted By: AndrewAMD
This doesn't work in normal C either.

s.P = malloc(sizeof(int));

Visual Studio says...
"a value of type "void *" cannot be assigned to an entity of type "int *"

Sounds like you compiled with C++ compiler.
C performs an implicit cast, whereas C++ requires an explicit cast for void* (static_cast<int*>)

Compiles in C

Code:
#include <stdlib.h>
#include <stdio.h>

struct S
{
	int* P;
};

int main()
{
	struct S s;
	s.P = malloc(sizeof(int));
	*(s.P) = 1234; // Ok
	s.P[0] = 1234; // Ok
	int* p = s.P;
	p[0] = 2345; // Ok
	printf("%d", *(s.P));
	free(s.P);
	return 0;
}


Last edited by pascalx; 09/03/17 14:40.
Re: lite-C bug [Re: pascalx] #467826
09/03/17 14:28
09/03/17 14:28
Joined: Aug 2017
Posts: 58
P
pascalx Offline OP
Junior Member
pascalx  Offline OP
Junior Member
P

Joined: Aug 2017
Posts: 58
Another Error. Or maybe just unsupported.

Code:
#define ARRAY_SIZE 10

int array1[10]; // Ok
int array2[ARRAY_SIZE]; // Ok
int array3[ARRAY_SIZE+1]; // Error

void main()
{
}


Last edited by pascalx; 09/03/17 14:40.
Re: lite-C bug [Re: pascalx] #467834
09/03/17 22:15
09/03/17 22:15
Joined: Aug 2017
Posts: 58
P
pascalx Offline OP
Junior Member
pascalx  Offline OP
Junior Member
P

Joined: Aug 2017
Posts: 58
Honestly guys, what kind of sorcery is this...

Code:
void f1()
{
	return 0; // compiles fine
}

int f2()
{
	return; // compiles fine
}



And this

Code:
void f()
{
	printf("A");
}

void f() // Ok
{
	printf("B");
}

void run()
{
	if(is(FIRSTRUN))
	{
		f(); // prints "B"
	}
}


Last edited by pascalx; 09/03/17 22:21.
Re: lite-C bug [Re: pascalx] #467835
09/04/17 00:16
09/04/17 00:16
Joined: Aug 2017
Posts: 58
P
pascalx Offline OP
Junior Member
pascalx  Offline OP
Junior Member
P

Joined: Aug 2017
Posts: 58
Code:
void main()
{
	const int i = 5;
	i = 6; // Ok
	printf("%d", i); // 6
}




Re: lite-C bug [Re: pascalx] #467856
09/04/17 18:07
09/04/17 18:07
Joined: Feb 2017
Posts: 1,718
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,718
Chicago
Welcome to Lite C!!! grin

In the following example, CASE1 is incorrect and CASE2 is correct syntax.

Code:
//#define CASE1
#define CASE2

int main()
{
	vars b[3];
	var c[3] = { 1.1, 1.2, 1.3 }; vars c1 = c;
	var d[3] = { 2.1, 2.2, 2.3 }; vars d1 = d;
	var e[3] = { 3.1, 3.2, 3.3 }; vars e1 = e;

	b[0] = c1;
	b[1] = d1;
	b[2] = e1;

	d[2] += 10.;
	e1[1] += 20.;

        int i, j;
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 3; j++)
		{
                      #ifdef CASE1
                      printf("%.2f ", b[i][j]); //dimension of array error
                      #endif
                      #ifdef CASE2
                      vars f;
                      f = b[i];
                      printf("%.2f ", f[j]); //this works!
                      #endif
		}
		printf("\n");
	}
	return 0;
}


Re: lite-C bug [Re: AndrewAMD] #467858
09/04/17 18:10
09/04/17 18:10
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
(b[i])[j] also works, if I remember correctly.


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: lite-C bug [Re: WretchedSid] #467859
09/04/17 18:18
09/04/17 18:18
Joined: Feb 2017
Posts: 1,718
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,718
Chicago
Originally Posted By: WretchedSid
(b[i])[j] also works, if I remember correctly.

Can confirm CASE3 works. Thanks for the tip!

Code:
//#define CASE1
//#define CASE2
#define CASE3

int main()
{
	vars b[3];
	var c[3] = { 1.1, 1.2, 1.3 }; vars c1 = c;
	var d[3] = { 2.1, 2.2, 2.3 }; vars d1 = d;
	var e[3] = { 3.1, 3.2, 3.3 }; vars e1 = e;

	b[0] = c1;
	b[1] = d1;
	b[2] = e1;

	d[2] += 10.;
	e1[1] += 20.;

        int i, j;
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 3; j++)
		{
                      #ifdef CASE1
                      printf("%.2f ", b[i][j]); //dimension of array error
                      #endif
                      #ifdef CASE2
                      vars f;
                      f = b[i];
                      printf("%.2f ", f[j]); //this works!
                      #endif
                      #ifdef CASE3
                      printf("%.2f ", (b[i])[j]);  //this works!
                      #endif
		}
		printf("\n");
	}
	return 0;
}


Re: lite-C bug [Re: AndrewAMD] #467860
09/04/17 18:36
09/04/17 18:36
Joined: Feb 2017
Posts: 1,718
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,718
Chicago
Fixing the opening example, CASE1 is incorrect syntax and CASE2 is correct.

Code:
//#define CASE1
#define CASE2

struct S
{
	int* P;
};

void main()
{
	struct S s;
	s.P = malloc(sizeof(int));
        *(s.P) = 1234; // Ok
        #ifdef CASE1
        s.P[0] = 1234; // Error: subscript requires array or pointer type
        #endif
        #ifdef CASE2
        (s.P)[0] = 1234; // Ok
        #endif
	int* p = s.P;
	p[0] = 2345; // Ok
	printf("%d", *(s.P));
	free(s.P);
}


Page 1 of 2 1 2

Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1