structure problem

K

kkk

Hi,

I am newbie to c programming. Currently I try to learn how to use
structure. So I create a structure named process and try to store
pointer of previous process as well as next. However, I encounter a
problem. The problem is - it seems the declaration of structure
current_process is not the previous one I want, but the current
process. What mistakes do I make in my programme?

I appreciate any suggestion

Below is my source code:

========== source code BEG ==========
#include <stdio.h>
#include <string.h>

struct process{
char name[10];
struct process *prev;
struct process *next;
};

void
init_pseudo_process_list(){
struct process p;
struct process *previous_process;
char n[10];
int i = 10;
for(i=0;i<10;i++){
memset(&p, 0, sizeof(&p));
snprintf(n, sizeof(n), "process_%d", i);
strncpy(p.name, n, sizeof(p));

if(i==0){/* int process */
p.prev = NULL;
previous_process = &p;
/*
printf("previous_process name:%s\n",
previous_process->name);
*/
}else{
if(i==1){
printf("previous_process name:%s\n",
previous_process->name);
}

p.prev = previous_process;
}
printf("name of current process: %s / prev process name: %s\n",
p.name, p.prev);
}
}

int
main(int argc, char **argv)
{
init_pseudo_process_list();

}

========== source code END ==========
 
?

=?Big5?B?Qmo/cm4gQXVnZXN0YWQ=?=

kkk said:
Hi,

I am newbie to c programming. Currently I try to learn how to use
structure. So I create a structure named process and try to store
pointer of previous process as well as next. However, I encounter a
problem. The problem is - it seems the declaration of structure
current_process is not the previous one I want, but the current
process. What mistakes do I make in my programme?

I appreciate any suggestion

Below is my source code:

========== source code BEG ==========
#include <stdio.h>
#include <string.h>

struct process{
char name[10];
struct process *prev;
struct process *next;
};

void
init_pseudo_process_list(){
struct process p;
struct process *previous_process;
char n[10];
int i = 10;
for(i=0;i<10;i++){
memset(&p, 0, sizeof(&p));
You probably want to do this only once?
snprintf(n, sizeof(n), "process_%d", i);


Why not snprintf() directly into p.name?
strncpy(p.name, n, sizeof(p));

sizeof(p) or sizeof(p.name)? ;-)


HTH
boa

[snip]
 
I

Ian Collins

kkk said:
Hi,

I am newbie to c programming. Currently I try to learn how to use
structure. So I create a structure named process and try to store
pointer of previous process as well as next. However, I encounter a
problem. The problem is - it seems the declaration of structure
current_process is not the previous one I want, but the current
process. What mistakes do I make in my programme?
In addition to the previous comments, you are only creating and updating
one process object, p. You probably want to change p to a process* and
allocate a new object (with calloc, to save your additional memset) each
loop iteration:

for( i=0; i<10; i++) {
process* p = calloc( 1, sizeof *p );

....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top