[comp.sys.mac.programmer] I thought I knew C, but...

STORKEL@RICE.BITNET (Scott Storkel) (05/27/88)

Okay, I give up. Could somebody please explain the following problem to me?
I tried to compile the following piece of code using  MPW C:

void convertfile()
{
     SFReply        myreply;
     Point          where;
     SFTypeList     mylist;

     where.v=40;
     where.h=40;
     mylist[0]="WORD";
     SFGetFile(&where, "File to convert?",nil,1,mylist,nil,&myreply);
}

Looks like it should put up an SFGetFile dialog box listing only MacWrite files
right? WRONG. When compiling, it gives a "warning: illegal combination of
pointer and integer" error on the line containing   mylist[0]="WORD". So, I
try all kinds of things to fix it, until I've almost driven myself crazy. Then
I change the offending line to the following:

     mylist[0]='WORD';

Just changing the double quotes to single quotes makes everything work fine. No
compiler warnings, and MacWrite files do  indeed show up in the dialog box.

So, my question is WHY? What is the big difference between single and double
quotes, and how do you know when to use them? If someone could please enlighten
me I would greatly appreciate it. Please e-mail responses directly to me if
possible.

Thanks,
Scott Storkel
Macintosh Software Development
Rice University
Houston, Texas

peter@aucs.UUCP (Peter Steele) (05/30/88)

> right? WRONG. When compiling, it gives a "warning: illegal combination of
> pointer and integer" error on the line containing   mylist[0]="WORD". So, I
> try all kinds of things to fix it, until I've almost driven myself crazy. Then
> I change the offending line to the following:
 
>      mylist[0]='WORD';
 
> Just changing the double quotes to single quotes makes everything work fine. No
> compiler warnings, and MacWrite files do  indeed show up in the dialog box.
 
> So, my question is WHY? What is the big difference between single and double
> quotes, and how do you know when to use them? 

There is a *big* difference between single and double quotes. Double quotes
create a "constant" pointer to an area of memory containing the indicated
characters. Such an expression consequently should be assigned to a pointer.
The file types list is *not* an array of pointers to char, it is an array
of 4 character vectors. The compiler was quite right to complain about your
first attempt. By changing the double quotes to single quotes, the problem
goes away because you are now creating an integer constant consisting of
four bytes--'W', 'O', 'R', and 'D'. Code like this would not necessarily
even compile on all systems. Whatever integer value this constant represents
is assigned directly to mylist[0], which is exactly what you want.

Sorry to post this. Our mailer rarely can find who to send messages to...


-- 
Peter Steele, Microcomputer Applications Analyst
Acadia University, Wolfville, NS, Canada B0P1X0 (902)542-2201x121
UUCP: {uunet|watmath|utai|garfield}dalcs!aucs!Peter
BITNET: Peter@Acadia  Internet: Peter%Acadia.BITNET@CUNYVM.CUNY.EDU

dan@Apple.COM (Dan Allen) (05/31/88)

The difference between double quotes and single quotes in C is that
double quotes create a zero-terminated string whereas single quotes
specify a literal.

It just so happens that in MPW C, literals can be anywhere from 1 to 4
characters in length, as four characters or bytes can fit in a longint.

Using four character literals is very important on the Macintosh because
of resource types, for example.  ResType is a four char literal stored
in a longint.

So anytime a file type or file creator or resource type is used on the
Mac, it is stored in a longint.  Thus using 'TEXT' is proper and "TEXT"
is wrong, in C.

Dan Allen
Software Explorer
Apple Computer

drc@dbase.UUCP (Dennis Cohen) (06/01/88)

In article <341STORKEL@RICE>, STORKEL@RICE.BITNET (Scott Storkel) writes:
> Okay, I give up. Could somebody please explain the following problem to me?
> I tried to compile the following piece of code using  MPW C:
> 
> void convertfile()
> {
>      SFReply        myreply;
>      Point          where;
>      SFTypeList     mylist;
> 
>      where.v=40;
>      where.h=40;
>      mylist[0]="WORD";
>      SFGetFile(&where, "File to convert?",nil,1,mylist,nil,&myreply);
> }
> 

As noted, it wanted single quotes around WORD rather than double quotes.  That
is because mylist[0] is a 4-byte quantity known as an OSType rather than a
string (in Pascal, Packed Array [0..3] of Char).  By putting single quotes
around WORD, the compiler treated it properly (:->) as a long, while with
double quotes, you had a 5-byte quantity (trailing null) which was aligned
improperly.  BTW:  This is a very common mistake among the C programmers I've
known who were new to the Mac.

Dennis Cohen
Ashton-Tate Macintosh Division
dBASE Mac Development Team
--------------------------
Disclaimer:  Opinions expressed above are _MINE_!

mz@well.UUCP (Michael Zentner) (06/02/88)

pardon me if this has been answered already, but

'doit'   is a 4 byte constant, each byte being the ascii representation of the
         respective character.

"doit"  is a string.


a function call such as  foo('doit') means you are passing a 4 byte constant.

a function call such as  foo("doit") means you pass an address of a character.


        

dc@gcm (Dave Caswell) (06/04/88)

In article <366@dbase.UUCP> drc@dbase.UUCP (Dennis Cohen) writes:
.
.As noted, it wanted single quote. around WORD rather than double quotes.  That
.is because mylist[0] is a 4-byte quantity known as an OSType rather than a
.string (in Pascal, Packed Array [0..3] of Char).  By putting single quotes
.around WORD, the compiler treated it properly (:->) as a long, while with
.double quotes, you had a 5-byte quantity (trailing null) which was aligned
.improperly.  BTW:  This is a very common mistake among the C programmers I've

No, it doesn't have anything to do with alignment. mylist = "word" assigns
the address of an array to mylist.  If mylist was char * rather than long
you could assign "any length phrase to it".  It doesn't have anything to
do with trailing nulls or the length of the phrase or alignment or anything
like that; and it certainly wouldn't work if you were to assign a 3 letter 
word to it  It isn't a problem of 4 byte versus 5 byte.  In the case of
mylist = "word" you are assigning the memory location where "word" is stored
rather than the characters 'word'.

-- 
Dave Caswell
Greenwich Capital Markets                             uunet!philabs!gcm!dc
If it could mean something, I wouldn't have posted about it! -- Brian Case