parse error before '*' token - c

const EPGState* NewEPGState[] =
{
&bootupState,
&standbyState,
&watchtvState,
&guideState,
&settingsState,
&easState,
&diagnosticsState
};
What is wrong in this code? I am getting an error parse error before '*' token
Your answers will be appreciated.

Check which version of the compiler are you using , This code compiles well on
g++ version 4.1.2 20071124.
but fails when compiled with gcc
I assume you have defined class/struct EPGState correctly and all the variables used below are of same type i.e EPGState.
class EPGState
{
};
int main()
{
EPGState bootupState,standbyState;
const EPGState* NewEPGState[] =
{
&bootupState,
&standbyState
};
}
Are you getting "parse Error" at the compilation step or at run time.
This is not clear from the question.
I written some sample code to test.
#include<iostream>
int main()
{
int a;
int b;
int *ac[]={&a,&b};
return 0;
}
Above code fails on gcc and compiles on g++.
gcc version 4.1.2 20071124 (Red Hat 4.1.2-42)
/tmp/cc6829sJ.o: In function __static_initialization_and_destruction_0(int, int)':
test1.cpp:(.text+0x4f): undefined reference to std::ios_base::Init::Init()

You probaly have
struct EPGState
{
...
struct EPGState bootupState =
{
...
somewhere.
Then is shall be
const struct EPGState * NewEPGState[] =
{
&bootupState,
...

Related

zig creates a C library but not usable by C

I'm able to get Zig to create a C library but when I attempt to use said library from a C program, it fails to find the definition of the included function.
My library definition:
const std = #import("std");
export fn removeAll(name: [*]const u8, len: u32) u32 {
const n: []const u8 = name[0..len];
std.fs.cwd().deleteTree(n) catch |err| {
return 1;
};
return 0;
}
test "basic remove functionality" {
}
build.zig
const Builder = #import("std").build.Builder;
pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const lib = b.addStaticLibrary("removeall", "src/main.zig");
lib.setBuildMode(mode);
switch (mode) {
.Debug, .ReleaseSafe => lib.bundle_compiler_rt = true,
.ReleaseFast, .ReleaseSmall => lib.disable_stack_probing = true,
}
lib.force_pic = true;
lib.setOutputDir("build");
lib.install();
var main_tests = b.addTest("src/main.zig");
main_tests.setBuildMode(mode);
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&main_tests.step);
}
zig build creates the build directory with the libremoveall.a static library.
My C program:
#include <stdio.h>
int removeAll(char *, int);
int main(int argc, char **argv)
{
removeAll("/tmp/mytest/abc", 15);
return 0;
}
When I attempt to include it in my C program, it get the following error:
gcc -o main build/libremoveall.a main.c
/usr/bin/ld: /tmp/cckS27fw.o: in function 'main':
main.c:(.text+0x20): undefined reference to 'removeAll'
Any ideas on what I'm doing wrong?
Thanks
EDIT
Thanks Paul R and stark, flipping the order worked. Can you help me understand why the order matter?
The linker searches a library for unresolved references encountered so far in the process.
If you put your program after the library, there are no unresolved references when the linker reads the library. And then when it reads your program, there is no library to resolve the reference.
Swap your program and the library, and you are all set.

GCC Compiler, Plugins and structures

I have scenario like this:
int open_ext2 () {}
int close_ext2 () {}
int read_ext2 () {}
int write_ext2 () {}
const struct fs_callbacks FS = {
open_file: open_ext2,
close_file: close_ext2,
read_bytes: read_ext2,
write_bytes: write_ext2
};
void main(){
FS.close_file();
}
When I look at the gimple representation (compiled with -fdump-tree-all)
I see something like this:
D.1796 = close_ext2;
D.1796 ();
What I do not get is where happens the assignment open_file: open_ext2
My questions
How GCC is doing this?
In what pass does it happen ?
Is there a way to figure out the mapping label -> member function?
Found the answer
The gcc option -fdump-tree-original-raw dumps the info
With GCC plugin:
Use the pass PLUGIN_FINISH_DECL
Have a look in GCC source for the function debug_variable in file: gcc/tree-dfa.c

Initializing struct within another struct using designated initializer causes compile error in Visual Studio 2013 [duplicate]

Using VS2013 Update 2, I've stumbled on some strange error message :
// test.c
int main(void)
{
struct foo {
int i;
float f;
};
struct bar {
unsigned u;
struct foo foo;
double d;
};
struct foo some_foo = {
.i = 1,
.f = 2.0
};
struct bar some_bar = {
.u = 3,
// error C2440 : 'initializing' : cannot convert from 'foo' to 'int'
.foo = some_foo,
.d = 4.0
};
// Works fine
some_bar.foo = some_foo;
return 0;
}
Both GCC and Clang accept it.
Am I missing something or does this piece of code exposes a compiler bug ?
EDIT : Duplicate: Initializing struct within another struct using designated initializer causes compile error in Visual Studio 2013
It is a known bug. It is said to be fixed in the next version of MSVC.
EDIT: Unfortunately, the bug is still present in VS14 CTP 4.
EDIT: This bug has been fixed in VS2015 CTP 5.

g++ compilation issue with array initialization

gcc compiles fine on the following code
enum AVMediaType {
AVMEDIA_TYPE_UNKNOWN = -1,
AVMEDIA_TYPE_VIDEO,
AVMEDIA_TYPE_AUDIO,
AVMEDIA_TYPE_DATA,
AVMEDIA_TYPE_SUBTITLE,
AVMEDIA_TYPE_ATTACHMENT,
AVMEDIA_TYPE_NB
};
static int wanted_stream[AVMEDIA_TYPE_NB]={
[AVMEDIA_TYPE_AUDIO]=-1, // Line 234
[AVMEDIA_TYPE_VIDEO]=-1,
[AVMEDIA_TYPE_SUBTITLE]=-1,
};
but g++ throws the following error
playerthread.cpp:234: error: expected primary-expression before '[' token
What's the issue here?
These kind of designated initializers aren't supported by g++, but they are by gcc. I'm not certain it's allowed in the C++ standard at all. You can see the same if you bring the code down to a very simple:
int array[10] = { [1] = 5 };
It's fine in C, not C++.

C struct problem

I am a C beginner, and I am curious why this gives me a Seg Fault everytime:
#include <stdio.h>
#include <stdlib.h>
struct Wrapper {
int value;
};
int main () {
struct Wrapper *test;
test->value = 5;
return 0;
}
I know I don't fully understand pointers yet, but I thought that
struct_ptr->field
is the same as
(*struct_ptr).field
so trying to make an assignment right to the field should be ok. This works like expected:
struct Wrapper test;
test.value = 5;
but I am curious why using the pointer causes a Seg Fault.
I am on Ubuntu 9.04 (i486-linux-gnu), gcc version 4.4.1
You didn't assign the pointer to anything. It's an uninitialized pointer pointing to who knows what, so the results are undefined.
You could assign the pointer to a dynamically created instance, like this:
int main () {
struct Wrapper *test;
test = (struct Wrapper *) malloc(sizeof(struct Wrapper));
test->value = 5;
free(test);
return 0;
}
EDIT: Realized this was C, not C++. Fixed code example accordingly.
You need to create an instance of Wrapper first:
struct Wrapper *test;
test = new struct Wrapper;
test->Value = 5;
Good luck.
You are using an uninitialised pointer, hence the segfault.
Catching this kind of error is possible, if you turn on some more warnings, using -Wall for example
You need to use -Wall in conjonction with some optimisation (-On) for the warning to appear. For instance, compiling your code with
gcc -Wall -O2 -c test.c
resulted in the following error message :
test.c: Dans la fonction «main» :
test.c:10: attention : «test» is used uninitialized in this function
While using french word, this compiler message is not an insult but a warning ;)
See below for a code allocating memory for your test pointer
int main () {
struct Wrapper *test;
test = malloc(sizeof(struct Wrapper))
if(test == NULL) {
/* error handling */
}
test->value = 5;
free(test)
return 0;
}

Resources