LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-12-2017, 05:17 AM   #1
srinietrx
Member
 
Registered: May 2013
Posts: 98

Rep: Reputation: Disabled
dereferencing pointer to incomplete type


Hi,
I am writing simple c program using structure and passing structure to function


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

void FunctionCall(void* tag);
int main()
{
struct s_tag
{
        int a;
        int *ptr;
};
struct s_tag *tag;

        tag = malloc(sizeof(struct s_tag));

        tag->a= 1;
        int b = 2;
        tag->ptr = &b;

        FunctionCall((void*)&tag);

        return 0;
}

void FunctionCall(void* tag)
{
        struct tag *my_tag =  (struct tag*)tag;
        printf("tag->a = %d\ttag->ptr = %d\n", my_tag->a, my_tag->ptr);
}
Getting error as
error: dereferencing pointer to incomplete type


Thanks
 
Old 12-12-2017, 05:39 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,559

Rep: Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092
Indeed, 'struct tag' is undefined.
Guess you wanted something like this:
Code:
#include <stdio.h>
#include <stdlib.h>

typedef struct s_tag
{
        int a;
        int *ptr;
} s_tag;

static void FunctionCall (const s_tag *tag);

int main()
{
	struct s_tag *tag;

        tag = malloc(sizeof(struct s_tag));

        tag->a= 1;
        int b = 2;
        tag->ptr = &b;

        FunctionCall(tag);

        return 0;
}

void FunctionCall (const s_tag *my_tag)
{
        printf("tag->a = %d\ttag->ptr = %p\n", my_tag->a, (void *)my_tag->ptr);
}

Last edited by NevemTeve; 12-12-2017 at 06:32 AM.
 
2 members found this post helpful.
Old 12-12-2017, 08:27 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170
But also: since you know that it is, and always will be, "a pointer to s_struct," you should declare it that way, not as void *.

You should declare a "void pointer" only when you literally do not know what sort of thing this pointer points to, and/or when it must be unpredictable and/or in the occasional edge-case where you really don't care. You want the compiler to be able to do just as much compile-time error checking as it possibly can.

Last edited by sundialsvcs; 12-12-2017 at 08:29 AM.
 
2 members found this post helpful.
Old 01-12-2018, 07:25 AM   #4
srinietrx
Member
 
Registered: May 2013
Posts: 98

Original Poster
Rep: Reputation: Disabled
Hi,
I corrected the issue myself immediately after posting. Sorry I could not reply immediately.

Regarding the answer. I did same as NevemTeve it is working i.e.declared structure outside the main.

Thanks NevemTeve,

sundialsvcs:-
In my application I was passing this Structure argument to thread. In every example program it is written as void*. So I did the same.
But I will correct it.
 
  


Reply

Tags
c programming


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
error: dereferencing pointer to incomplete type answerme Programming 4 09-18-2013 05:45 AM
dereferencing pointer to an incomplete type veena hosur Linux - Newbie 2 03-13-2013 06:29 AM
error: dereferencing pointer to incomplete type joyce092130 Programming 11 09-29-2012 09:06 AM
error: dereferencing pointer to incomplete type joyce092130 Programming 1 07-07-2012 09:57 AM
dereferencing pointer to incomplete type sweetymobil Programming 7 06-22-2009 07:46 AM

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

All times are GMT -5. The time now is 03:40 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration