newbie print struct C programming question - c

I am new in C programming, and trying to create simple code below for printing a struct member using other function.
I do not understand this, as in the function funct_to_print_value, I already declare the struct variable "car", and I believe what I need is just to print is using (dot) notation to access it. Appereantly not, as I got the error above. Does anyone can share their knowledge, how I can print the value of buyer, and what mistake I had done above?
Thank you ..
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct slot_car {
int buyer;
} slot_car;
int main() {
slot_car car;
memset(&car, 0, sizeof(car));
car.buyer = 1;
printf("value of car is .. %d\n", car.buyer);
funct_to_print_value();
printf("end of function..\n");
return 0;
}
int funct_to_print_value()
{
printf("you are in printlist function..\n");
slot_car car;
printf("value of car inside is %d\n", car.buyer);
return 1;
}

Since you declared car inside each function separately, they are separate (local) variables. You probably want to pass it from main to funct_to_print_value as a parameter instead. The warning is strange, but it is possible that the compiler detected the uninitiated value and gave this message because it is first used in printf.

This looks fine to me. It would be helpful to have more information. All I did was extract your example into a temp.c file and compile it using gcc -c temp.c. There were no errors.
On which OS is this?
Which build environment/compiler is this?
How are you building this? (commands used in the build environment)
I'm using gcc 4.4.3 on Ubuntu Linux 10.04.
Edit 1:
What happens if you cast car.buyer to an int?
printf("value of car is .. %d\n", (int ) car.buyer);
Edit 2:
How about this to print your 1?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct slot_car {
int buyer;
} slot_car;
int main() {
slot_car car;
memset(&car, 0, sizeof(car));
car.buyer = 1;
printf("value of car is .. %d\n", car.buyer);
{
int temp_ret;
temp_ret = funct_to_print_value();
printf("end of function..%d\n",temp_ret);
}
return 0;
}
int funct_to_print_value()
{
printf("you are in printlist function..\n");
slot_car car;
printf("value of car inside is %d\n", car.buyer);
return 1;
}
value of car is .. 1
you are in printlist function..
value of car inside is 134514096
end of function..1
cnorton#steamboy:~/scratch$

Related

Why is Cod::Blocks giving me Null instead of a input char?

Source Code:
#3 main.c
# include "func.h"
int main(void) {
func();
return 0;
}
#3 func.h
#include <stdio.h>
void inputName();
void printName();
void func();
#3 func.c
#include "func.h"
char GLOBAL_NAME;
void inputName() {
scanf("%s", &GLOBAL_NAME);
}
void printName() {
printf("Your name is: %s.\n", &GLOBAL_NAME);
}
void func(void) {
inputName();
printName();
}
Out Put:
Your name is: (null).
I used https://www.online-cpp.com/online_c_compiler with the same code, it works fine on the online compiler. but when I try to use it on Code::Blocks it shows me:
Your name is: (null).
Don't know what's the problem, Could it be a compiler thing?
I'm using a windows machine for Code::Blocks using GCC I think as the compiler.
Initialize your char variable with a length and since you have not initialized it with a length it returns NULL.
char GLOBAL_NAME[30];

XCode EXC_BAD_ACCESS by Changing Variable Name

Take this simple program and run it in Xcode:
#include <stdio.h>
#include "inttypes.h"
struct {
int32_t nwgts;
;
} clbint_;
#define clbint_1 clbint_
int main()
{
printf("clbint_1.nwgts: %d\n",clbint_1.nwgts);
clbint_1.nwgts=1; //Crash here
printf("clbint_1.nwgts: %d\n",clbint_1.nwgts);
return 0;
}
And when you run it you will get a crash on the line indicated that just says Thread 1: EXC_BAD_ACCESS (code=2, address=0x10078e18c). But if you change clbint_ to clbinta_ like this:
#include <stdio.h>
#include "inttypes.h"
struct {
int32_t nwgts;
;
} clbinta_;
#define clbint_1 clbinta_
int main()
{
printf("clbint_1.nwgts: %d\n",clbint_1.nwgts);
clbint_1.nwgts=1;
printf("clbint_1.nwgts: %d\n",clbint_1.nwgts);
return 0;
}
The results print exactly as I expect:
clbint_1.nwgts: 0
clbint_1.nwgts: 1
I am trying to understand why this is happening, clearly it has something to do with the variable name clbint_ but why does this behavior occur?

How to use a function of a struct in another struct?

i'm doing a simple project in C in which, for the moment, i use two structs: Player and Card.
I have created the 2 struct in different header files, because functions in Player use Card, but also other elements that I haven't already done.
When I try to use getId() in Player, the VSCode's compiler says:
reference to external symbol _getId not resolved in _discardCard()
card.h code is:
#include <stdbool.h>
typedef struct card{
int id;
bool black;
int numElems;
char* text[3];
}card;
card* initCard(int id,bool black,char* text[],int numElems)
int getId(card* c);
card.c code is:
#include "carta.h"
#include<stdio.h>
#include <stdbool.h>
#include <stdlib.h>
...
int getId(carta* c){
return c->id;
}
Instead the code for Player.h is:
#include "card.h"
#define CARDSMAX 5
typedef struct{
bool master;
int id;
int points;
char* nickname;
card* cards[CARDSMAX];
int NumCards;
}player;
... //other functions
card* discardCard(int id,player* g);
The code for Player.c is:
#include "player.h"
#include <stdio.h>
#include <stdlib.h>
player* initPlayer(char* nickname,int id){
player* g=(player*) malloc(sizeof (player));
g->id=id;
g->nickname=nickname;
g->master=false;
g->points=0;
g->NumCards=0;
for(int i=0;i<CARDSMAX;i++){
g->cards[i]=(card*)malloc(sizeof(card));
}
return g;
}
....
card* DiscardCard(int id,player* g){
for(int i=0;i<CARDSMAX;i++){
card* c=g->cards[i];
if(getId(c)==id){
card* e= g->cards[i];
g->cards[i]=NULL;
g->NumCards--;
return e;
}
}
return NULL;
}
Can someone help me? (If you see some inconsistency in the code, it'is because i tried a fast translation from my language)
So, as suggested , the problem was that in Windows' compiler, when you use struct that use function defined in other structs,it is required to pass the .obj file during the compilation .
For doing this you need to compile every .c file that is used by others singulary and then link the .obj file created in this fase.
So in my case the first step is to compile:
cl card.c
that create a card.obj (the Windows' compiler could show you a message that says something like:"The start point is not indicated" if you don't use a main in this first file, don't worry and carry on with this procedure) and then I compile:
cl player.c /link card.obj
So in this way you pass the compiled object in which it's provided the implementation of all the function in the struct.

Why isn't my function invoked? I don't understand why a declaration is expected

Hello folks out there,
this is my code:
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#include "db_typedefs.h"
#include "operations.h"
int main(){
printf("Text\n");
int f = 3;
void add_mini(3);
}
operations.h
#ifndef ADD_OPERATIONS_H_INCLUDED
#define ADD_OPERATIONS_H_INCLUDED
void add_mini(int flag);
#endif // ADD_OPERATIONS_H_INCLUDED
operations.c
void add_mini(int flag)
{
int rc;
rc = flag;
printf("%i\n", rc);
}
Operations.c has also libraries included similar to main.c.
Compiler Error
error: expected declaration specifiers or '...' before numeric constant
regarding to void add_mini(3)
It seems like I'm unable to pass a simple integer value. While debugging it's even skipping the add_mini line.
Do you have any idea what's going on?
The whole code is embedded in a larger query to determine typed orders but this works fine. I just can't pass this simple integer value.
Thanks in advance.
When you use
void add_mini(3);
the compiler thinks it is a function declaration, not a function call. The argument 3 is not valid for a function declaration. Hence, the compiler complains.
Remove the void part to call the function.
int main(){
printf("Text\n");
int f = 3;
add_mini(3);
}
or, since you have initialized f to 3,
int main(){
printf("Text\n");
int f = 3;
add_mini(f);
}
Call the function like so: add_mini(3); rather than void add_mini(3);
Remove the word void for calling add_mini from main.c :
add_mini(3);
Or
(void)add_mini(3);

C program compiles but no output

I am trying to learn creating header file in C and including it in my main.c func() . I created a simple tut1.c file with function named call() and a tut1.h header file which externs tut1.c function named call(). Thats it, now i am using eclipse Juno for C/C++ on linux fedora. I dont get any compile error but the code wont output? I tried on console and eclipse in vain. Can you check please? Thanks
---main.c-----
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "tut1.h"
int main (void)
{
int tut1(void);
return 0;
}
-----tut1.c------
#include <stdio.h>
#include <stdlib.h>
#include "tut1.h"
int call (void)
{
int *ptr;
int i;
ptr = &i;
*ptr = 10;
printf ("%d we are printing the value of &i\n", &i);
printf ("%d we are printing the value of *ptr\n", *ptr);
printf ("%d we are printing the value of ptr\n", ptr);
printf ("%d we are printing the value of &ptr\n", &ptr);
getchar();
return 0;
}
----tut1.h----
#ifndef TUT1_H_
#define TUT1_H_
extern int call (void);
#endif
You're not seeing anything because you're not calling the call() function from your main() function.
The main() function is the default entry point when you run the program, i.e. the first function that gets called during execution.
To execute the function call() you would need to call this from main() as follows :
int main (void)
{
int result = call();
return 0;
}
BTW, this line int tut1(void); within your main() just declares a function, which you do not seem to have defined anywhere. So I have removed it in the above shown code.

Resources