Hi,
I have a struct array in my application. I want to call a function, change
values of struct variables and return back to main - while still keeping
the information written in the function.
I thought I'd do it as pasted below but my problem is that my thread
terminates whenever i access SumStruct_ptr's elements to read/write values
and i have no clue why, could anyone help please?
Thanks alot! -
typedef struct {
-
char * BusID;
-
int SeqNr;
-
unsigned short Msg;
-
} MsgStruct;
-
-
int StructCount=0;
-
int CheckStruct (MsgStruct **SumStruct_ptr, io_t *CurrentBus)
-
{
-
prs_log(LOG_CRIT,"***in CheckStruct, sizeof(%d), StructCount:%d",
-
sizeof(SumStruct_ptr), StructCount);
-
int i=0;
-
if (StructCount==0){
-
prs_log(LOG_CRIT,"***in if, StructCount: %d,", StructCount);
-
(*SumStruct_ptr) = calloc( StructCount + 1, sizeof( MsgStruct ) );
-
prs_log(LOG_CRIT,"***sucessfully allocated space: %d",
-
sizeof((*SumStruct_ptr)));
-
(*SumStruct_ptr)[StructCount].BusID=CurrentBus->id;
-
(*SumStruct_ptr)[StructCount].SeqNr=CurrentBus->seq;
-
(*SumStruct_ptr)[StructCount].Msg=0x00;
-
StructCount++;
-
prs_log(LOG_CRIT,"***Values in SumStruct_ptr[%d]: BusID:%s seqNr:%d
-
Msg:0x%x", StructCount, (*SumStruct_ptr)[StructCount].BusID,
-
(*SumStruct_ptr)[StructCount].SeqNr, (*SumStruct_ptr)[StructCount]);
-
}
-
// some more code to extend the array
-
}
-
-
main(void)
-
{
-
static MsgStruct *SumStruct;
-
Pos = CheckStruct(&SumStruct,_cur_io);
-
SumStruct[Pos].Msg|=TSP_SERVICE_REQUEST;
-
}
-
--
weeks of software enineering safe hours of planing ;) 2 1021
Ron Eggler <unkn...@example.comwrote:
...
typedef struct {
* * * * char * BusID;
* * * * int SeqNr;
* * * * unsigned short Msg;
* * * * } MsgStruct;
int StructCount=0;
int CheckStruct (MsgStruct **SumStruct_ptr, io_t
*CurrentBus)
{
prs_log(LOG_CRIT,"***in CheckStruct, sizeof(%d),
StructCount:%d",sizeof(SumStruct_ptr), StructCount);
%d is not appropriate for a size_t.
BTW, some indentation wouldn't hurt.
int i=0;
if (StructCount==0){
I'm straining (and wondering why I'm bothering), but I can't
see a balancing }.
prs_log(LOG_CRIT,"***in if, StructCount: %d,", StructCount);
(*SumStruct_ptr) = calloc( StructCount + 1, sizeof( MsgStruct ) );
Here you allocate 1 MsgStruct. It's simpler to just write
*SumStruct_ptr = malloc(sizeof *SumStruct_ptr);
(*SumStruct_ptr)[0] = 0;
Realise that all bits zero needn't yield a null pointer.
prs_log(LOG_CRIT,"***sucessfully allocated space: %d",
sizeof((*SumStruct_ptr)));
(*SumStruct_ptr)[StructCount].BusID=CurrentBus->id;
(*SumStruct_ptr)[StructCount].SeqNr=CurrentBus->seq;
(*SumStruct_ptr)[StructCount].Msg=0x00;
Here you access [0] which is fine.
StructCount++;
StructCount is now 1.
prs_log(LOG_CRIT,"***Values in SumStruct_ptr[%d]: BusID:%s seqNr:%d
Msg:0x%x", StructCount, (*SumStruct_ptr)[StructCount].BusID,
(*SumStruct_ptr)[StructCount].SeqNr, (*SumStruct_ptr)[StructCount]);}
Here you access [1], which is isn't fine.
// some more code to extend the array
"No lieutenant, your men are already dead."
>
}
main(void)
Implicit int was deprecated by C90 and removed in C99.
int main(void)
{
static MsgStruct *SumStruct;
Pos = CheckStruct(&SumStruct,_cur_io);
SumStruct[Pos].Msg|=TSP_SERVICE_REQUEST;}
[/code]
--
Peter
On Tue, 03 Jun 2008 18:29:14 GMT, Ron Eggler <un*****@example.com>
wrote:
>Hi,
I have a struct array in my application. I want to call a function, change values of struct variables and return back to main - while still keeping the information written in the function. I thought I'd do it as pasted below but my problem is that my thread terminates whenever i access SumStruct_ptr's elements to read/write values and i have no clue why, could anyone help please?
snip 30+ lines of horribly unindented code.
Your code still invokes the same undefined behavior it did when you
posted a previous version in alt.comp.lang.c. You only corrected half
the errors pointed out then and I doubt if many will continue to read
the eye test you present here.
Remove del for email This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ken |
last post by:
Hi all. When referring to a null pointer constant in C++, is there
any reason to prefer using 0 over a macro called NULL that is defined
to be 0?
Thanks!
Ken
|
by: __PPS__ |
last post by:
Hello everybody
in a quiz I had a question about dangling pointer:
"What a dangling pointer is and the danger of using it"
My answer was:
"dangling pointer is a pointer that points to some...
|
by: randomtalk |
last post by:
hello everyone!
Well, recently i've been trying to pick up c and see what is pointer
all about (been programming in lisp/python for the better part of my
last two years)..
mmm.. I'm currently...
|
by: David Mathog |
last post by:
This one is driving me slightly batty. The code in question is
buried deep in somebody else's massive package but it boils down to
this, two pointers are declared, the first is:
char **resname...
|
by: Scott |
last post by:
Hi All,
I can't seem to wrap my head around this one.
I have a pointer,
int *x;
which I can assign:
|
by: elmar |
last post by:
Hi Clers,
If I look at my ~200000 lines of C code programmed over the past 15
years, there is one annoying thing in this smart language, which
somehow reduces the 'beauty' of the source code...
|
by: James Brown |
last post by:
All,
After reading the FAQ (Q 5.2) I see that:
an "integral constant expression with the value 0" in a pointer context is
converted to a null pointer at compile time. The FAQ goes on to say...
|
by: xdevel |
last post by:
Hi, if I have:
int a=100, b = 200, c = 300;
int *a = {&a, &b, &c};
than say that:
int **b is equal to int *a is correct????
|
by: matevzb |
last post by:
I've ran into some fishy code that, at first glance, is buggy, but it
seems to work correctly
and none of the compilers I've tried (five so far, on various systems)
gives any warnings.
The code:...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |