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];
Related
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);
For unit testing I'd like to replace a function from "outside". Normally, I'm using the wrapping mechanism - but unfortunately this does not work for calls to the function from within the same compilation unit.
My idea was to mark the function as "weak" so I am able to reimplement it in a testing application. Generally this works using the following code:
File myfunctions.c (this is the code under test):
#include "myfunctions.h"
int weakFunction(int param) __attribute__((weak));
int weakFunction(int param)
{
return 2*param;
}
int myfunction(int param)
{
int result = weakFunction(param);
return (result == (2*param)) ? 1:0;
}
File main.c
#include "myfunctions.h"
int weakFunction(int param)
{
return 3*param;
}
int main()
{
return myfunction(5);
}
This example works as expected - when I remove weakFunction from main.c, the program returns 1, when I add weakFunction the program returns 0. Looks good at this point.
But as soon as I change the order within myfunctions.c as follows, the resulting program crashes with a segmentation fault:
File myfunctions.c (modified order):
#include "myfunctions.h"
int weakFunction(int param) __attribute__((weak));
int myfunction(int param)
{
int result = weakFunction(param);
return (result == (2*param)) ? 1:0;
}
int weakFunction(int param)
{
return 2*param;
}
Any idea? What could be the reason for the crash?
I am using GCC 4.8.1 (MinGW w64 build) on Windows 7.
Thanks for any help!
Florian
I wrote a code and it compiles perfectly in CodeBlocks 13.12 with no errors !
i copied the same code to VS2010 it shows 1 error:
IntelliSense: identifier "malloc" is undefined
CodeBlocks Code:
#include <stdio.h>
#define maxLength 4
typedef short int *set;
void func(set *a)
{
*a=malloc(maxLength*sizeof(set));
(*a)[0]=10;
(*a)[1]=13;
(*a)[2]=15;
}
void main()
{
set a;
func(&a);
printf("%d %d %d",a[0],a[1],a[2]);
}
VS2010 Code:
#include "stdafx.h"
#include <stdio.h>
#define maxLength 4
typedef short int *set;
void func(set *a){
*a=malloc(maxLength*sizeof(set));
(*a)[0]=10;
(*a)[1]=13;
(*a)[2]=15;
}
int _tmain(int argc, _TCHAR* argv[])
{
set a;
func(&a);
printf("%d %d %d",a[0],a[1],a[2]);
return 0;
}
I don't know what is the problem ..
and if i add in the pre-compile code : #include <iostream>
the error goes , but another error appears:
IntelliSense: a value of type "void *" cannot be assigned to an entity of type "set"
NEW CODE
void func(set *a){
func(a);
}
void func1(set *a){
*a=reinterpret_cast<set>(malloc(maxLength*sizeof(set)));
(*a)[0]=10;
(*a)[1]=13;
(*a)[2]=15;
}
How do i create an array in a function that called by a function ?
There are two problems you seem to have. The first and most serious is that you're missing a header file. See e.g. this malloc reference. Not including this header file will cause your memory allocations to now work as you expect.
The other error is that you actually seem to be using C++ and not C. In C you should not cast the return of malloc, but in C++ you must do it.
You should add a header file defining the malloc function: stdlib.h (C) or cstdlib (C++).
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.
I am having problem with importing external function to a main c file.
Here is my minimal code:
/* main.c */
#include<stdio.h>
extern int func()
int main(){
extern int func();
}
/*external file with one function that I want to
import*/
#include<stdio.h>
int func(){
printf("Hello World Again\n");
}
I compile and run like this - gcc main.c and then ./a.out but nothing is happening.
Any idea ?
You have to compile the file containing func also
gcc -Wall main.c external_file.c
(Note that the -Wall in the compiler command isn't absolutely necessary but is very good practice)
As noted by others, you also need to fix your code to call func rather than just re-declaring it.
Because you only declared the function, You never called it!
extern int func();
Declares a function. To call it you must have:
int main()
{
func();
}
You are just declaring again in main function..
you need to call the function to work..#include
extern int func()
int main(){
func();
}
/*external file with one function that I want to
import*/
#include<stdio.h>
int func(){
printf("Hello World Again\n");
}
Edits: question has changed.
extern is only used for external variables. You just need a prototype for the function.
#include <stdio.h>
void func(void); /* <-- prototype */
int main(int argc, char * argv[])
{
func();
return 0;
}
void func(void){
printf("Hello World Again\n");
}
Notice a few things. A prototype of int func() means no parameter checking in C - this is different to C++. Also, you are not returning anything from the function, so I replace it with void func(void)