Jump to content
IGNORED

Dumb C question


Recommended Posts

Hey C programmings. I have a question for you.

I'm playing around with Pure C and attempting to print the return value of Logbase(). At first I was getting the error, "Non-Portable pointer assignment". I think I solved this by making the return point to a Char *. I chose this because, as I think I understand it, VoidP return type means it returns a pointer to a char.

  • Question #1; Why the Atari Compendium doesn't just state the return type as a char pointer.

So, they I got to print it but I get this error message, "'scrn' is assigned but never used in function main".

This isn't hard to understand but I AM using it. Here is part of the code...

 

int main(void)
{
    char * scrn;
    open_vwork();
    v_clsvwk(handle);
    printf("\nTesting for resolution\n");
    printf("GetRes is : %d\n", Getrez());
    printf("V_Clsvwk X res: %d. Y res %d\n", work_out[0] + 1, work_out[1] + 1);
    scrn = Logbase();
    printf("Address of logical base location: %p", scrn);
    Cconin();
    return(0);
}
  • Question #2: I'm printing out scrn so why is it telling me I'm not using it?

 

Thanks,

Link to comment
Share on other sites

No expert on pureC, but;

 

What do the docs say Logbase() should return, as a type? (EDIT) OK found it and is is type VOIDP, or char* so that should be OK.

 

Also you haven't declared "handle", Line 5

or work_out in line 8

 

How do you define the functions you are using, open_vwork(), Getrez(), Logbase()? I don't see any "#include" statements

also "#include <stdio.h>" is missing

 

Maybe post the full error output.

Edited by Sub(Function(:))
Link to comment
Share on other sites

Oh, that was just a portion of the code. Here is the full thing.

#include <stdio.h>
#include <tos.h> 
#include <stdlib.h>
#include <vdi.h>
#define TRUE 1 
#define FALSE 0
/***********PROTOTYPES**************/
void open_vwork(void);

/*************GLOBALS***************/
int work_in[11];
int work_out[57];
int handle;

/**************MAIN****************/

int main(void)
{
	char * scrn;
	open_vwork();
	v_clsvwk(handle);
	printf("\nTesting for resolution\n");
	printf("GetRes is : %d\n", Getrez());
	printf("V_Clsvwk X res: %d. Y res %d\n", work_out[0] + 1, work_out[1] + 1);
	scrn = Logbase();
	printf("Address of logical base location: %p", scrn);
	Cconin();
	return(0);
}

void open_vwork(void)
{
	int i;
	for (i=0; i<10; work_in[i++] = 1);
	work_in[10] = 2;
	v_opnvwk(work_in, &handle, work_out);
}
                     
Link to comment
Share on other sites

A void * should be different from a char *, in that you can't dereference it, and you don't need a cast to convert it to other pointer types. That's just compiler semantics though, the generated code would be the same for both pointer types.

 

Are you sure that printf() is defined to do anything useful in the stdio.h you are using? If it was an empty macro, that compile message would make sense.

Link to comment
Share on other sites

Sorry, you kinda lost me a bit but I think I figured it out. What I think was throwing me was my understanding of VoidP. Void, to me, is "nothing" so I couldn't understand how you could return a void pointer...until I actually looked into it and finally found this...

 

Void Pointer Example :
void *ptr; // ptr is declared as Void pointer
char cnum;
int inum;
float fnum;
ptr = &cnum; // ptr has address of character data
ptr = &inum; // ptr has address of integer data
ptr = &fnum; // ptr has address of float data

 

So, I just changed the declaration to void * scrn and it doesn't look like I'm getting any warnings anymore.

Link to comment
Share on other sites

Yeah a void* doesn't really mean "pointer to nothing" it is more like "pointer to something, and we don't know what that something is".

 

When you have a pointer to a data type, you can do arithmetic on it, and dereference it:

 

int intarray[10] = {1,2,3,4,5,6,7,8,9,10};

int *intptr = intarray; /* intptr points to 1 */

intptr++; /* intptr now points to 2 */

int foo = *intptr; /* foo is 2 */

int bar = intptr[5]; /* bar is 7 */

 

You can't (or at least shouldn't be able to) do the last three lines to a void*, because the compiler doesn't know that it points to. You could copy the pointer around and recast it though.

 

Does that make sense? Sorry if there are any typos. I used to write C code for a living, but that was a long time ago ;)

Edited by galax
Link to comment
Share on other sites

Yes.
I actually watched some videos on it and it made more sense when I looked into malloc more. For instance...

 

int *A = (int*)malloc(sizeof(int));

 

That casting made total sense but something like Logbase() returns a pointer to a location in memory. A memory address would be hex and I know you can print a hex value using %x, but as far as I know, that's not a type so you can't cast it to a type. Best I can figure at this time is you can cast it to a string but I think I would have to assign that to an array of chars, right?

 

Of course, if I want to print it, I just use the %p so it pretty much solves that problem. Using void just seams like having a method return type object...and I just don't like that at all. Of course, I know there are uses for that. I just haven't run into that yet in Java.

Link to comment
Share on other sites

Yes.

I actually watched some videos on it and it made more sense when I looked into malloc more. For instance...

 

int *A = (int*)malloc(sizeof(int));

 

That casting made total sense but something like Logbase() returns a pointer to a location in memory. A memory address would be hex and I know you can print a hex value using %x, but as far as I know, that's not a type so you can't cast it to a type. Best I can figure at this time is you can cast it to a string but I think I would have to assign that to an array of chars, right?

 

Of course, if I want to print it, I just use the %p so it pretty much solves that problem. Using void just seams like having a method return type object...and I just don't like that at all. Of course, I know there are uses for that. I just haven't run into that yet in Java.

 

Right, a memory address is not 'hex'...its a positive integer number. Hexadecimal is a printed representation of a number. You could print an address out with %x, %d, %p, or %i for instance. You could actually use %s on it of course, as printf doesn't pay attention to types, but it would be garbage.

 

You'll run into lots of usage of char * to point to arbitrary blocks of memory, don't assume that means it is a string. Char * actually ought to be 'byte *' because that's what it really is. Void * returns are just handy to explicitly indicate that the return could be to lots of different things, or to untyped things, and as was mentioned it invokes some compiler behavior to simplify casting. I've seen lots of 'get screen memory address' routines that return char *.

 

My advice is to try not to make comparisons to Java when you are trying to understand C data structures and types, they are wildly different.

Edited by danwinslow
Link to comment
Share on other sites

Oh, I wasn't trying to compare Java to C. It's just that Java is the language I've been spending most of my time in so I used that as a comparison when equating a generic type, such as object, to voidp, which seems liked a generic type itself.

Java's syntax is like C but probably shares more with C++. In any case, I understand the differences between the two but since I haven't touched C since 1992, I just need to dust off all of those memories. The good news is that I still remember a good portion of my 68K assembly so it's not like I'm far removed from understanding the inner workings of C.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...