petFoo
petFoo

Reputation: 437

Pointers giving errors when building an old unix C project in Xcode?

I have an old C project that compiles just fine in Unix. I want to build it in Xcode on Lion, so I created a new Xcode project and added all of the files (except for the makefile).

At any rate, I am running into a number of 'Semantic Issues' while building. Does anyone know what could be going on here? All of these problems with pointers, seems odd. Is it really that different building a project in Xcode than using make? Does this have something to do with osx?

For example:

unsigned char *p = b->buf + adbuf_used(b);

gives the error:

Initializing 'unsigned char *' with an expression of type 'char *' converts between pointers to integer types with different sign

And

printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));

gives the error:

Conversion specifies type 'int' but the argument has type 'unsigned

And

printf("%2d, ", p-cset);

gives the error:

Conversion specifies type 'int' but the argument has type 'long'

And

if (getsockname(sock_rt, (struct sockaddr *)&iface_out, &len) == -1 ) {

gives the error:

Passing 'int *' to parameter of type 'socklen_t *' (aka 'unsigned int *') converts between pointers to integer types with different sign

Upvotes: 2

Views: 2104

Answers (2)

MByD
MByD

Reputation: 137392

The errors from the compilers are shown because this compiler is more strict, and by definition, it is right. In the cases you showed, you can change the following:

  1. p and b->buff are not of the same type, p is of type unsigned char *, while b->buff is of type char *

    unsigned char *p = b->buf + adbuf_used(b);
    // To
    char *p = b->buf + adbuf_used(b);
    // or
    unsigned char *p = (unsigned char *)b->buf + adbuf_used(b);
    
  2. The format specifier for unsigned integers is %u, not %d. Or, as commented, in the case of size_t use %zu

    printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
    // to
    printf("sizeof(unsigned int) = %zu\n", sizeof(unsigned int));
    
  3. The format specifier for long is %ld, not %d

    printf("%2d, ", p->cset);
    // to
    printf("%2ld, ", p->cset);
    
  4. The 3rd parameter is of type socklen_t *, not int *

    if (getsockname(sock_rt, (struct sockaddr *)&iface_out, &len) == -1 )
    //instead of 
    int len;
    // define
    socklen_t len;
    

All those cases will usually cause no harm if stayed as-is, but basically unsigned and signed are different types, and long and int are different types (it actually matters in many systems)

Upvotes: 3

dirkgently
dirkgently

Reputation: 111250

At least one of b->buf and adbuf_used(b) is either a plain char or a signed char.

The specifier for unsigned integers in printf is u.

The specifier d assumes the argument is of type int whereas p-cset (?) is of type long. Use l instead.

The getsockname is again a similar enough error which you can figure out by yourself.

Read the manual for printf. And honor compiler warnings.

Upvotes: 1

Related Questions