By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

structure problem

P: n/a
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 ==========

Feb 18 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
kkk wrote:
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]
Feb 18 '07 #2

P: n/a
kkk wrote:
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 );

....

--
Ian Collins.
Feb 18 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.