Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8736937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:24:34+00:00 2026-06-13T10:24:34+00:00

In the following program I try to pass a structure to a function. But

  • 0

In the following program I try to pass a structure to a function. But I get errors,and I do not understand why. What mistake have I made in this program ?

I am using gcc for compiling this c program.

#include <stdio.h>

struct tester {
  int x;
  int *ptr;
};

void function(tester t);

int main() {
 tester t;
 t.x = 10;
 t.ptr = & t.x;
 function(t);
}

void function(tester t) {
   printf("%d\n%p\n",t.x,t.ptr);
}

Errors :

gcc tester.c -o tester

tester.c:8:15: error: unknown type name ‘tester’
tester.c: In function ‘main’:
tester.c:12:2: error: unknown type name ‘tester’
tester.c:13:3: error: request for member ‘x’ in something not a structure or union
tester.c:14:3: error: request for member ‘ptr’ in something not a structure or union
tester.c:14:13: error: request for member ‘x’ in something not a structure or union
tester.c: At top level:
tester.c:18:15: error: unknown type name ‘tester’

NOTE : If I replace printf with cout and stdio with iostream and name the extension to .cpp (!), I get no errors. Why is that ? No wonder I compile it using g++

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:24:35+00:00Added an answer on June 13, 2026 at 10:24 am

    If you don’t typedef the struct you must specify struct in front of the struct name while declaring it like so:

    struct tester t;
    

    Either you do that or you do the following:

    typedef struct {
      int x;
      int *ptr;
    }tester;
    

    Update

    Below is a quote from Adam Rosenfield from the following post Difference between ‘struct’ and ‘typedef struct’ in C++?:

    In C++, all struct/union/enum/class declarations act like they are implicitly typedef’ed, as long as the name is not hidden by another declaration with the same name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following program. However, I can't understand why I have to pass
When I try to use SDL in my c++ program, I get the following:
When I try and run my program I get this warning and some weird
I have the following program but it wrongly displays the month. please help. date1=31/12/2011;
Following program runs fine with Netbeans IDE but when i try to run from
the following program force quit and crashes, I don't understand why, import android.app.Activity; import
In following program . I have one doubt. I have declared one global variable
Consider the following code: class Program { static void Main(string[] args) { try {
I have the following problem with a program which I wrote in Visual C++
My teacher gave me this question: Write a program that does the following: Enter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.