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 ==========
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 ==========