Click here to Skip to main content
15,748,748 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <stdio.h>
#include <stdlib.h>
struct test
{
    int id;
    char name[20];
};
int main()
{

struct test t1;

   t1.id=1;
   fflush(stdin);
   fgets(t1.name,20,stdin);

print(&t1.name);
print1(t1.id,&(t1.name));

}
void print(struct test *name)
{
    puts(name);
}

void print1(struct test id,struct test *name)
{

    printf("\n%d\n",id);
    puts(name);
}


What I have tried:

the first puts in print function works perfectly but why doesnt it works in second function in print1?
Posted
Updated 10-Jul-16 5:19am
Comments
Kornfeld Eliyahu Peter 10-Jul-16 6:32am    
Did you checked it with the debugger?

Simple: it's not a struct test that you pass to the method, it's a string.
Your functionm expects a pointer to a struct:
C++
void print1(struct test id,struct test *name)
{
    printf("\n%d\n",id);
    puts(name);
}
but you pass this:
C++
print1(t1.id,&(t1.name));

Which is a char *:
C#
struct test
{
    int id;
    char name[20];
};

And you pass an integer as the first value as well!
I'm not sure what you are trying to do here - I think you want to re-read your course notes and try again!
 
Share this answer
 
Comments
wedtorque 10-Jul-16 6:44am    
why does the first function is working ?
regarding what i want to do
let consider a structure with 5 member .i want to send the first member as a value and 2nd member by refernce to a function .how would i do that ?
OriginalGriff 10-Jul-16 6:53am    
void MyFunction(int id, char** pstrName)
{
...
}
...
MyFunction(t1.id, &(t1.name));

Or just pass a pointer to the whole struct:
void MyFunction(struct test* data)
{
...
}
...
MyFunction(&t1);
wedtorque 10-Jul-16 6:58am    
thanks :) this solves my question
as OriginalGriff already said, the parameters *name are not structures, they are *char.

You should go back to C/C++ basics:
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

Use the debugger to see what your code is doing.
The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900