i have this piece of code
struct atmel_tc *atmel_tc_alloc(unsigned block, const char *name)
{
struct atmel_tc *tc;
/* Iterate over the list elements */
list_for_each_entry(tc, &tc_list, node) {
/* Do something with tc */
}
[...]
}
this is from a kernel source file, I'm a bit new to C so I'm unable to understand
this line struct atmel_tc *atmel_tc_alloc(unsigned block, const char *name)
as so far I've seen structs declared simply as struct atmel_tc
without any parameters or * , I've seen usages like
struct node {
int data;
struct node *next;
}*head;
where *head is a pointer to an instance of node. Is something similar going on here as well?
could you explain this line or point me towards some documentation for similar kind of syntax
struct atmel_tc *atmel_tc_alloc(unsigned block, const char *name)
This says that atmel_tc_alloc is a function that returns a pointer to a struct atmel_tc and takes as parameters an unsigned named block and a const char * called name.
Related
I am new to C and as a (classic) exercise I am trying to implement some operations on linked lists. I haven't gotten very far yet, though... When I am trying to declare and initialize the root node as follows:
#include <stdlib.h>
struct intNode_t {
int data;
struct intNode_t* next;
};
int main() {
struct intNode_t* root = ( intNode_t* ) malloc( sizeof( struct intNode_t ) );
return 0;
}
the compiler (clang) is giving me the error "use of undeclared identifier" at the place where I am trying to typecast the void pointer returned by malloc to a pointer to intNode_t. I realise this is a noob question, but I couldn't find the answer elsewhere. Any suggestions?
The names of structs in C occupy a separate namespace from the space of names of fundamental types and typedefs (see this question for details). Therefore, the name of your struct type is struct intNode_t; there is no type called intNode_t.
You can either always spell out the name as struct intNode_t, or you can create a type alias for it. There are many different patterns of this in real code:
Separate tag name, separate declaration:
typedef struct foo_t_ foo_t;
struct foo_t_ { /* ... */ };
This allows you to put the type alias in a header and publish it as an opaque API type without ever revealing the actual type definition.
Separate tag name, typedef in struct definition:
typedef struct foo_t_ { /* ... */ } foo_t;
Reuse name:
typedef struct foo_t { /* ... */ } foo_t;
This style allows users to be careless about spelling foo_t or struct foo_t.
Don't name the struct:
typedef struct { /* ... */ } foo_t;
However, in your code you don't actually need to repeat the name, since you should not cast the result of malloc and instead rely on the built-in implicit conversion from void pointer to object pointer. You also shouldn't repeat the type that's already known, and use the expression form of sizeof instead. So you want:
int main()
{
struct intNode_t* root = malloc(sizeof *root);
}
(Also note that return 0 is implied from main.)
No need to repeat yourself. If you don't repeat yourself you can make fewer errors.
struct intNode_t *root;
root = malloc( sizeof *root );
the cast is not needed; malloc() returns a void*, which is exchangeable with every (non-function) pointer type.
there are two syntax variants for sizeof: sizeof(type) and sizeof expression The first one needs (), the second one does not. Most people prefer the second form, because the expression (in your case *root ) always will yield the correct type, and thus: size.
And, the definition + assignment above can be combined into a definition + initialiser, all fitting on one line:
struct intNode_t *root = malloc( sizeof *root );
Name intNode_t was not declared in the program. There is declared structure tag name intNode_t.
So you need to write
struct intNode_t* root = ( struct intNode_t* ) malloc( sizeof( struct intNode_t ) );
Or you could introduce identifier name intNode_t the following way
typedef struct intNode_t {
int data;
struct intNode_t* next;
} intNode_t;
In this case you may write
struct intNode_t* root = ( intNode_t* ) malloc( sizeof( struct intNode_t ) );
I'm doing a Forth interpreter in C. I can't decide how to better implement the Forth dictionary.
struct Word {
struct Word* next;
char* name;
int* opcode;
// int arg_count;
}
struct Dictionary {
struct Word words;
int size;
}
opcode is a sequence of codes - functions of word. So each opcode[i] corresponds to some function. I suppose it should be some table with elements [opcode<->function pointer]. But how to implement it?
We don't know the size of function. We can't use void* (or we can?) since we must somehow having only opcode execute the function.
What should I do?
Some variation on this definition is quite common in traditional Forth implementations:
typedef int cell;
typedef void code_t (struct Word *);
struct Word
{
char name[NAME_LENGTH];
struct Word *next;
code_t *code;
cell body[]; /* Upon instantiation, this could be zero or more items. */
};
The dictionary will then be a list linked through the next pointer. The words are allocated sequentially, interleaving the struct Word header, and the body data.
To execute a word, call word->code(word);. The function pointed to by code can then decide what to do with body. The body could be data, or it could be what you call "opcodes".
A colon defintion will have code pointing at something like this:
void docolon (struct Word *word)
{
/* IP is a variable holding the instruction pointer. */
rpush (IP); /* Push the current instruction pointer to the return stack. */
IP = (struct Word *)word->body; /* Start executing the word body (opcodes). */
}
Whereas a primitive word, e.g. + would look like
void plus (struct Word *word)
{
cell n1 = pop();
cell n2 = pop();
push (n1 + n2);
}
All of this below is based on an assumption: You want to declare function pointers.
typedef int (*OPCODE)(char *);
struct Word
{
struct Word* next;
char* name;
OPCODE *opcode;
// int arg_count;
};
opcode is a function pointer to a function that returns an integer and takes a char * as the argument. A really good page of short tutorials on function pointers is The Function Pointer Tutorials by Lars Engelfried.
I have this:
typedef struct nodebase{
char name[254];
char sex;
int clientnum;
int cellphone;
struct nodebase *next;
struct nodebase *encoding;
} clientdata;
I have added clientdata *curr[]; in seperate function. The reason why I made *curr into *curr[] instead is that this client data will be stored in a .txt file. So I came up with singly linked-list to read all the data and when the program fscanf every 5th variable, I will add 1 to clientcounter.
So, the *curr[] will be *curr[clientcounter].
Now, I need to convert this pointer array into char array named temp[clientcounter] because char array is needed to evaluate something else later in the code.
I came up with this code below:(Using Tiny C on Windows)
void loaded_data_transfer(clientdata *curr,clientdata temp[],int clientcounter)
{
clientdata temp[] = {0};
temp[clientcounter].name = curr[clientcounter]->name;
temp[clientcounter].sex = curr[clientcounter]->sex;
temp[clientcounter].clientnum = curr[clientcounter]->clientnum;
temp[clientcounter].cellphone = curr[clientcounter]->cellphone;
}
The problem is, Tiny C is giving me an error: lvalue expected at temp[clientcounter.name = ... part. Can anyone tell me what did I do wrong?
And if anyone knows a better way to keep track of the curr of clientdata by using counter and by using singly linked-list, please let me know.
You cannot assign an array to another. You should use strcpy or strncpy
strcpy(temp[clientcounter].name, curr[clientcounter]->name);
Maybe you meant to copy the entire struct:
void loaded_data_transfer(clientdata * curr, clientdata temp[], int clientcounter)
{
temp[clientcounter] = *curr; // Copy entire struct
}
It should work, because your struct doesn't any pointer members.
I am assuming you use it like this
clientdata * curr[CURR_SIZE];
clientdata temp[TEMP_SIZE];
/* init curr elements here */
loaded_data_transfer(*curr[clientcounter], temp, clientcounter);
Also, your declaration should be:
void loaded_data_transfer(clientdata *curr[],...
I have trouble with structures in c.
I have two structures like
typedef struct
{
char isim[256];
int deger;
struct ekstra *sonra;
}ekstra;
typedef struct
{
char *name;
int val;
struct ekstra *next;
}node;
/*and main is*/
int main()
{
int i;
node dizi[12];
for(i=0;i<12;i++)
{
dizi[i].name = malloc("asdasd"*sizeof(int));
strcpy (dizi[i].name,"asdasd");
/*and trouble starts here*/
**dizi[i].next = malloc(sizeof(ekstra));
printf("%s",dizi[i].next->isim);**
}
}
the error is
error: dereferencing pointer to incomplete type
How can I hold place for dizi[i].next?
struct ekstra is not the same as ekstra.
Your first struct typedef should be declared as follows:
typedef struct ekstra
{
char isim[256];
int deger;
struct ekstra *sonra;
}ekstra;
typedef struct... ekstra;
This means: "create a type that is called ekstra". From now on this type can be used just as any variable type (int, char etc).
struct ekstra *next;
This means: Somewhere in my program there is a struct of some type, I don't know what it contains, but I want to point to an element of that type. This is the meaning of incomplete type.
To fix your problems, simply replace this row with ekstra *next;.
More comments not directly related to the question:
dizi[i].name = malloc("asdasd"*sizeof(int));
This is pure nonsense code. It means: "Create a constant string literal in the ROM part of my program. In this constant string literal, store the letters "asdasd" and a null termination character. Then take the address of this ROM memory location, which is completely irrelevant to my application, convert it to an integer so that I get a 32-bit nonsense number. Then multiply this nonsense number with the sizeof an int, which doesn't make any sense to anyone either. Then take this completely nonsense result and allocate a random amount of dynamic memory based on this. Then watch the program crash.
I don't understand code line like
malloc("asdasd"*sizeof(int));
But, I think you problem should slove like this
dizi[i].next = (ekstra *)malloc(sizeof(ekstra));
and you struct define should like
typedef struct node{
int a;
int b;
struct node *next;
}node;
BIG EDIT:
Ok, my original question didn't help me. Here is a second go.
My struct looks like this:
struct node {
char *name;
int age;
struct node *nextName;
struct node *nextAge;
};
I have to make two linked lists out of structures like this,.
So i have 'rootAges' which keeps track of where the Age-based list starts and
'rootNames' which keeps track of where the names start. I can't seem to get these to update.
that is, i have struct node *rootAges and struct node *rootNames.
I need to pass both of these to a function which adds the elements to the list.
But i also need the roots to change as I add things to the list.
the methods provided so far, haven't changed the value of rootAges for example in the main function, when it is altered in the add function.
Thanks!
You pass the address of a structure's instance to a function that accepts a pointer in C.
void fn(struct data *p)
{
if(p)
p->x = 33;
}
//... main ...
struct data d;
fn(&d);
//d.x == 33
Pass a pointer to the structure.
For example:
typedef struct data{ int x;} s_data;
void foo (s_data* pointer){}
s_data s={0};
foo(&s);
Declaration
void Foo(struct data *);
Definition
void Foo(struct data *p)
{
//body
}
In your code
Foo(root);