473,387 Members | 1,465 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,387 software developers and data experts.

pointer confusion - any hints?

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!
Expand|Select|Wrap|Line Numbers
  1. typedef struct {
  2. char * BusID;
  3. int SeqNr;
  4. unsigned short Msg;
  5. } MsgStruct;
  6.  
  7. int StructCount=0;
  8. int CheckStruct (MsgStruct **SumStruct_ptr, io_t *CurrentBus)
  9. {
  10. prs_log(LOG_CRIT,"***in CheckStruct, sizeof(%d), StructCount:%d",
  11. sizeof(SumStruct_ptr), StructCount);
  12. int i=0;
  13. if (StructCount==0){
  14. prs_log(LOG_CRIT,"***in if, StructCount: %d,", StructCount);
  15. (*SumStruct_ptr) = calloc( StructCount + 1, sizeof( MsgStruct ) );
  16. prs_log(LOG_CRIT,"***sucessfully allocated space: %d",
  17. sizeof((*SumStruct_ptr)));
  18. (*SumStruct_ptr)[StructCount].BusID=CurrentBus->id;
  19. (*SumStruct_ptr)[StructCount].SeqNr=CurrentBus->seq;
  20. (*SumStruct_ptr)[StructCount].Msg=0x00;
  21. StructCount++;
  22. prs_log(LOG_CRIT,"***Values in SumStruct_ptr[%d]: BusID:%s seqNr:%d
  23. Msg:0x%x", StructCount, (*SumStruct_ptr)[StructCount].BusID,
  24. (*SumStruct_ptr)[StructCount].SeqNr, (*SumStruct_ptr)[StructCount]);
  25. }
  26. // some more code to extend the array
  27. }
  28.  
  29. main(void)
  30. {
  31. static MsgStruct *SumStruct;
  32. Pos = CheckStruct(&SumStruct,_cur_io);
  33. SumStruct[Pos].Msg|=TSP_SERVICE_REQUEST;
  34. }
  35.  
--
weeks of software enineering safe hours of planing ;)
Jun 27 '08 #1
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
Jun 27 '08 #2
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
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

69
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
20
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...
3
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...
3
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...
6
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:
49
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...
13
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...
42
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????
17
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:...
0
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,...
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:
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...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
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...
0
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...

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.