I am trying to upload a solution to an OJ ,the judge uses GCC
I have received the following errors and having no idea about them.
error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘{’ token
void insert(int in){
^
In function ‘main’:
error: ‘struct mymultiset_int’ has no member named ‘insert’
x.insert(t);
^
error: ‘struct mymultiset_int’ has no member named ‘getmax’
printf("%d\n",x.getmax());
^
error: ‘struct mymultiset_int’ has no member named ‘_delete’
x._delete(0);
^
My code looks like this:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int t;
#define swap(a,b) t=b,b=a,a=t
/*
when using swap, I use format like swap(x,y); or swap(x,y),
*/
struct mymultiset_int{
int e[100000],end;
void insert(int in){...}
int getmax(){ return e[0]; }
void _delete(int i){...}
}x;
int main(){
x.end=0;memset(x.e,0,sizeof(x.e));
int N,t;scanf("%d",&N);
char i[2];
while (N--){
scanf("%s",i);
if (i[0]=='A'){
scanf("%d",&t);
x.insert(t);
}
else{
printf("%d\n",x.getmax());
x._delete(0);
}
}
}
you cannot define function in structure in C.
But it is possible in c++.
Related
I am trying to call a function that is declared as a function ptr I believe? Compiler Gives error: expected ‘)’ before ‘,’ token
static void map_start(struct map_reduce *mr){
map_fn(mr, mr->infile, mr->id , mr->threadNum);
}
static void reduce_start(struct map_reduce *mr){
reduce_fn(mr, mr->outfile,(mr->threadNum));
}
//map_fn and reduce_fn is defined as these
typedef int (*map_fn)(struct map_reduce *mr, int infd, int id, int nmaps);
typedef int (*reduce_fn)(struct map_reduce *mr, int outfd, int nmaps);
errors are:
mapreduce.c: In function ‘map_start’:
mapreduce.c:87: error: expected ‘)’ before ‘,’ token
mapreduce.c: In function ‘reduce_start’:
mapreduce.c:92: error: expected ‘)’ before ‘,’ token
make: *** [mapreduce.o] Error 1
Hey Guys I got a Compile Error in my C Project with a Function Pointer.
Thank you for your help :)
Folder Structure:
Main Folder:
enter image description here
Lib Folder:
enter image description here
Heder Folder:
Path : C-Lib/Headers
File(s) : ShowPointer.h
Code (Main.c):
#include <stdio.h>
#include "./Headers/ShowPointer.h"
int main(){
printf("Hej this is written in VIM some C code.\n");
getchar();
}
Code (ShowPointer.c):
#include <stdio.h>
#include "../Headers/ShowPointer.h"
void ExPointer(int *pPointer, int *pPointerMax){
for (int i = *pPointer; i<*pPointerMax; i++){
printf("%d. %d %p\n", i, *pPointerMax-i, pPointerMax);
}
getchar();
}
Code (ShowPointer.h):
#ifndef SHOWPOINTER_FILE
#define SHOWPOINTER_FILE
typedef void ExPointer (*)(int , int);
#endif
Compile:
I do Compile this Project whit this code:
gcc -o main main.c Lib/ShowAddress.c
Error:
Out come of the Compile error text (code):
In file included from main.c:2:0:
./Headers/ShowPointer.h:4:25: error: expected declaration specifiers or ‘...’ before ‘*’ token
typedef void ExPointer (*)(int , int);
^
In file included from Lib/ShowAddress.c:2:0:
Lib/../Headers/ShowPointer.h:4:25: error: expected declaration specifiers or ‘...’ before ‘*’ token
typedef void ExPointer (*)(int , int);
Sorry for me bad English.
You want
typedef void (*ExPointer)(int , int);
How to create a typedef for function pointers
I have this function:
void simul(char *new)
{
segment_table seg = malloc(sizeof(st));
segcode newcode = malloc(sizeof(sc));
int clen = 0;
seg -> segname = new;
clen = code_length(seg -> segname);
seg -> code = read_hexcode(seg -> segname, newcode, clen);
load_image(seg, clen-3);
if (strstr(new, "paper_boot-strap loader.hexcode") == NULL)
run(segment_offset(seg));
}
When I try to compile my program I get the following errors:
error: ‘st’ undeclared (first use in this function)
error: ‘sc’ undeclared (first use in this function)
error: invalid type argument of ‘->’ (have ‘segment_table’)
error: invalid type argument of ‘->’ (have ‘segment_table’)
error: invalid type argument of ‘->’ (have ‘segment_table’)
error: invalid type argument of ‘->’ (have ‘segment_table’)
What is it that I am doing wrong?
Header file, for example header.h:
// First an include guard (https://en.wikipedia.org/wiki/Include_guard)
#ifndef HEADER_
#define HEADER_
// Define the structures and their type-aliases
typedef struct st { ... } st;
typedef struct sc { ... } sc;
// Declare a couple of other type-aliases
// (Note that I personally recommend against declaring pointer type-aliases)
// (I personally recommend against using type-aliases for simple types
// like the above structures too)
typedef st *segment_table;
typedef sc *segcode;
// Declare a function prototype
void simul(char *new);
#endif // HEADER_
One source file
#include "header.h"
void simul(char *new)
{
...
}
Second source file
#include "header.h"
int main(void)
{
sumul("some string");
}
I want to pass a string > 1024 chars to my module (filesystem).
As kernel parameters are limited to 1024 chars, someone recommended to use sysfs instead.
I tried to include this example in my super.c class to create a string 'filename' & string 'code' entry in sysfs for my module.
static decl_subsys(myfs, NULL, NULL);
struct myfs_attr {
struct attribute attr;
char *value;
};
static struct myfs_attr fname = {
.attr.name="filename",
.attr.owner = THIS_MODULE,
.attr.mode = 0644,
.value = "/my/test/path",
};
static struct myfs_attr code = {
.attr.name="code",
.attr.owner = THIS_MODULE,
.attr.mode = 0644,
.value = "0101",
};
When compiling my module I get a lot of errors (line 41 is decl_subsys):
fs/myfs/super.c:41:26: error: expected ‘)’ before ‘(’ token
fs/myfs/super.c:50:2: error: unknown field ‘owner’ specified in initializer
fs/myfs/super.c:50:2: warning: initialization from incompatible pointer type [enabled by default]
fs/myfs/super.c:50:2: warning: (near initialization for ‘fname.attr.name’) [enabled by default]
...
fs/myfs/super.c: At top level:
fs/myfs/super.c:83:15: error: variable ‘myfsops’ has initializer but incomplete type
fs/myfs/super.c:84:2: error: unknown field ‘show’ specified in initializer
fs/myfs/super.c:84:2: warning: excess elements in struct initializer [enabled by default]
fs/myfs/super.c:84:2: warning: (near initialization for ‘myfsops’) [enabled by default]
fs/myfs/super.c:85:2: error: unknown field ‘store’ specified in initializer
fs/myfs/super.c:85:2: warning: excess elements in struct initializer [enabled by default]
fs/myfs/super.c:85:2: warning: (near initialization for ‘myfsops’) [enabled by default]
fs/myfs/super.c:89:2: error: unknown field ‘myfs_ops’ specified in initializer
fs/myfs/super.c:89:2: warning: initialization from incompatible pointer type [enabled by default]
fs/myfs/super.c:89:2: warning: (near initialization for ‘myfstype.release’) [enabled by default]
fs/myfs/super.c: In function ‘init_myfs_fs’:
fs/myfs/super.c:1554:2: error: implicit declaration of function ‘kobj_set_kset_s’ [-Werror=implicit-function-declaration]
fs/myfs/super.c:1554:19: error: ‘myfs_subsys’ undeclared (first use in this function)
fs/myfs/super.c:1554:19: note: each undeclared identifier is reported only once for each function it appears in
fs/myfs/super.c:1554:32: error: ‘fs_subsys’ undeclared (first use in this function)
fs/myfs/super.c:1557:2: error: implicit declaration of function ‘subsystem_register’ [-Werror=implicit-function-declaration]
fs/myfs/super.c: In function ‘exit_myfs_fs’:
fs/myfs/super.c:1579:2: error: implicit declaration of function ‘subsystem_unregister’ [-Werror=implicit-function-declaration]
fs/myfs/super.c:1579:24: error: ‘myfs_subsys’ undeclared (first use in this function)
Is this tutorial just outdated for my 3.5 kernel or am I missing something else?
How could I create 2 char string entries for my module in sysfs?
Here is the source code for transmitting data to a "param_buf" string. As requested, without read method. Only store.
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <asm/string.h>
static struct kobject *register_kobj;
static char *param_buf;
// function for many symbol data enter
static ssize_t __used store_value(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count){
printk(KERN_ALERT "you entered %s\n", buf);
strncpy(param_buf, buf, PAGE_SIZE - 1);
return count;
}
// register function to attribute
static struct kobj_attribute store_val_attribute = __ATTR( put_parameters, 0220, NULL, store_value);
// put attribute to attribute group
static struct attribute *register_attrs[] = {
&store_val_attribute.attr,
NULL, /* NULL terminate the list*/
};
static struct attribute_group reg_attr_group = {
.attrs = register_attrs
};
static int hello_init(void){
param_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
// create sysfs object ( /sys/kernel/test_1025_sym directory )
register_kobj = kobject_create_and_add("test_1025_sym", kernel_kobj);
if (!register_kobj)
return -ENOMEM;
//create attributes (files)
if(sysfs_create_group(register_kobj, ®_attr_group)){
kobject_put(register_kobj);
return -ENOMEM;
}
return 0;
}
static void hello_exit(void){
printk(KERN_ALERT "last value was %s\n", param_buf);
kfree(param_buf);
kobject_put(register_kobj);
}
MODULE_LICENSE("Dual BSD/GPL");
module_init(hello_init);
module_exit(hello_exit);
You can test it in such a way:
cat /etc/fstab > /sys/kernel/test_1025_sym/put_parameters
For two string entries: copy a store_value function, register one more store_val_attribute and put it to attributes list.
I am working on an example from book - CSPP - O'Hallaron & Bryant
on compile time i get an error -
gcc -Wall -W -Werror main.c -o endianc
also tried with cc main.c - same error
aaron#aaron-Box:~/xxx/cpp/endian$ cc main.c
main.c: In function ‘main’:
main.c:6:24: error: storage class specified for parameter ‘byte_pointer’
typedef unsigned char *byte_pointer;
^
main.c:8:17: error: expected declaration specifiers or ‘...’ before ‘byte_pointer’
void show_bytes(byte_pointer start, int len)
^
main.c:17:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
main.c:22:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
main.c:27:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
main.c:6:24: error: declaration for parameter ‘byte_pointer’ but no such parameter
typedef unsigned char *byte_pointer;
^
main.c:29:1: error: expected ‘{’ at end of input
}
actual program -
#include <stdio.h>
#include<stdlib.h>
int main(void)
{
typedef unsigned char *byte_pointer;
void show_bytes(byte_pointer start, int len)
{
int i;
for (i = 0; i < len; i++)
printf(" %.2x", start[i]);
printf("\n");
}
void show_int(int x)
{
show_bytes((byte_pointer) &x, sizeof(int));
}
void show_float(float x)
{
show_bytes((byte_pointer) &x, sizeof(float));
}
void show_pointer(void *x)
{
show_bytes((byte_pointer) &x, sizeof(void *));
}
return 0;
}
thanks for your help.
You are missing the semicolon ; after void main(). (Also, it seems unlikely that you would need that line in the first place, but you need the actual implementation of the main function, and it has a return type of int, not void.)
Missing semicolon at main() that is why it is throwing the error.
The error is happening because "void main()" is immediately followed by typedef. The entire body of the main function is missing. Go back and check the book, and see what you left out.
ok i have fixed it now
replaced int main(void); with int main() notice NO simicolon -> i used it from this tutorial -> URL
and also replaced return 0; with return (0); notice the parenthesis with return.
i also noticed the examples in "c Programming Language" D.Richie -> example with main method do not include ;
1) can i please ask what are these variations in code ?
2) now i can not run the program ./endianc -> it has no response
i also tried
xx#aaron-Box:~/xxx/cpp/endian$ cc main.c
xx#aaron-Box:~/xxx/cpp/endian$ a out
a: command not found
(do i need to open separate thread for this problem?)
thanks all for your help.