473,382 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

structure problem

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
2 1315
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Muhammad Farooq-i-Azam | last post by:
Hi, I am trying to define an arp structure but having problem doing so. I think I have define the correct arp structure but I find myself in a strange problem. The size of structure that I have...
3
by: Leo Nunez | last post by:
Hello! I need copy from structure "A" to "B" that contains "strings" in a one line code. Me problem like this : typedef struct tHeader{ char field1; char field2; char field3;
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
2
by: Steve Turner | last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows: typedef struct tag_scanparm { short...
15
by: Charles Law | last post by:
I have adapted the following code from the MSDN help for PropertyInfo SetValue. In the original code, the structure MyStructure is defined as a class MyProperty, and it works as expected. There is...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
11
by: Lance | last post by:
Hi all, I've got a some structures defined as ////// <StructLayout(LayoutKind.Sequential)Public Structure GM_LayerInfo_t Public mDescription As String Public mNativeRect As GM_Rectangle_t...
3
by: =?Utf-8?B?Umljb2hEZXZlbG9wZXI=?= | last post by:
I have a class library project that uses unmanaged C dll to perform some image handling. This DLL requires me to pass in a structure containing image coordinates. In VB6, I could use a...
4
by: eBob.com | last post by:
In my class which contains the code for my worker thread I have ... Public MustInherit Class Base_Miner #Region " Delegates for accessing main UI form " Delegate Sub DelegAddProgressBar(ByVal...
3
by: =?Utf-8?B?TWluZ3lp?= | last post by:
Hi, I have the following question regarding communicating with a OCX control using C#. Idon't have access to the ocx code, so my c# code is actually mimic the behavior of C++ counterparts....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.