LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-07-2014, 11:01 PM   #1
godna
LQ Newbie
 
Registered: Nov 2014
Posts: 2

Rep: Reputation: Disabled
Warning: pointer type mismatch


Hi all, I'm new programming in C, so I had the next message in my code:

Dual.c:88:20: warning: pointer type mismatch in conditional expression [enabled by default] : &clientSa.sin6.sin6.sin6_addr,

Any help would be great

Code:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc,const char **argv)
{
  int serverPort = 1234;
  int rc;
  union {
    struct sockaddr_in sin;
    struct sockaddr_in6 sin6;
  } serverSa; 
  union {
    struct sockaddr_in sin;
    struct sockaddr_in6 sin6;
  } clientSa;

  int clientSaSize = sizeof(clientSa);
  int on = 1;
  int family;
  socklen_t serverSaSize;
  int c;
  char buf[INET6_ADDRSTRLEN];

  int s = socket(PF_INET6,SOCK_STREAM,0);
  if (s < 0)
  {
    fprintf(stderr, "IPv6 not active, falling back to IPv4...\n");
    s = socket(PF_INET,SOCK_STREAM,0);
    if (s < 0)
    {
      perror("socket failed");
      exit (1);
    }
    family = AF_INET;
    serverSaSize = sizeof(struct sockaddr_in);
  }
 else  /* got a v6 socket */
  {
    family = AF_INET6;
    serverSaSize = sizeof(struct sockaddr_in6);
  }
  printf("socket descriptor is
 rc = setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&on,sizeof on);

  /* initialize the server's sockaddr */
  memset(&serverSa,0,sizeof(serverSa));
  switch(family)
  {
    case AF_INET:
      serverSa.sin.sin_family = AF_INET;
      serverSa.sin.sin_addr.s_addr = htonl(INADDR_ANY);
      serverSa.sin.sin_port = htons(serverPort);
      break;
    case AF_INET6:
      serverSa.sin6.sin6_family = AF_INET6;
      serverSa.sin6.sin6_addr = in6addr_any;
      serverSa.sin6.sin6_port = htons(serverPort);
  }
  rc = bind(s,(struct sockaddr *)&serverSa,serverSaSize);
  if (rc < 0)
  {
    perror("bind failed");
    exit(1);
  }
  rc = listen(s,10);
  if (rc < 0)
  {
    perror("listen failed");
    exit(1);
  }
  rc = accept(s,(struct sockaddr *)&clientSa,&clientSaSize);
  if (rc < 0)
  {
    perror("accept failed");
    exit(1);
  }
  c = rc;
  printf("Client address is: %s\n",
       inet_ntop(clientSa.sin.sin_family,
                 clientSa.sin.sin_family == AF_INET
                   ? &clientSa.sin.sin_addr
                   : &clientSa.sin6.sin6_addr,
                 buf, sizeof(buf)));

  if(clientSa.sin.sin_family == AF_INET6
    && ! IN6_IS_ADDR_V4MAPPED(&clientSa.sin6.sin6_addr))
    printf("Client is v6\n");
  else
    printf("Client is v4\n");

  rc = write(c,"hello\n",6);
  close (s);
  close (c);
  return 0;
}
 
Old 11-08-2014, 05:13 AM   #2
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware, Slarm64 & Android
Posts: 16,241

Rep: Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321
Usual cause of that one is that you have defined a variable, and used it as a pointer. The exact error message would give you a line number. M$ compilers just fix it and shut up, but the gnu gcc has gone to lengths to minimize all the C errors which result from sloppy coding, and can cause bugs.

When one is new that is a major pita. Try this link
http://www.tutorialspoint.com/cprogr...c_pointers.htm
 
Old 11-08-2014, 07:08 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
The ? : construct requires that its second and third arguments be the same data type.
(A ? B : C) requires B and C to be the same type and only a limited amount of automatic casting will occur to get them to match. Otherwise it is the programmer's responsibility.

The inet_ntop function expects its second parameter to be void*, which so far as I recall would normally be automatically cast from another pointer type.

So the confusing issue might be that automatic casts in C are always determined bottom up, using no top down information. The fact that the result of (A ? B : C) would be cast to void* does not inform the compiler that each of B and C should be cast to void*. Instead the compiler first tries to automatically cast B and C to the same pointer type. Only if that succeeds would it cast the result to void*.

The simple solution is to force the casts to void* by adding the code I showed here in red.

Code:
                 clientSa.sin.sin_family == AF_INET
                   ? (void*) &clientSa.sin.sin_addr
                   : (void*) &clientSa.sin6.sin6_addr,

Last edited by johnsfine; 11-08-2014 at 07:11 AM.
 
Old 11-08-2014, 10:49 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Let's note that there is an union sockaddr, which az union of different sockaddr types.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Java - "Type mismatch: cannot convert from element type Object to int[]" Infasoft Programming 2 07-09-2010 01:06 PM
gcc 4.0.0 warning: dereferencing type-punned pointer will break strict-aliasing rules stephenwalter Programming 4 06-10-2009 04:06 AM
warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type eCli Programming 1 01-19-2009 07:44 AM
list<type> how can I make type be a pointer? exodist Programming 2 06-06-2005 08:40 AM
What is the warning: passing arg 3 of `pthread_create' from incompatible pointer type wallwaters Linux - Software 3 06-01-2005 08:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration