| | |
simple structure question
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include <stdio.h> struct FUN { char x; char *y; int z[20]; }; int main(void) { struct FUN fn1; struct FUN fn2; struct FUN fn3[10]; struct FUN fn4[50]; return 0; }
is an assignment like fn4[23] = fn3[5] invalid?
•
•
•
•
C++ Syntax (Toggle Plain Text)
#include <stdio.h> struct FUN { char x; char *y; int z[20]; }; int main(void) { struct FUN fn1; struct FUN fn2; struct FUN fn3[10]; struct FUN fn4[50]; return 0; }
is an assignment like fn4[23] = fn3[5] invalid?

Set up a function to move the parts of FUN from one value to another.
> is an assignment like fn4[23] = fn3[5] invalid?
Well it's fine as far as the syntax is concerned.
However, the pointer inside the structure presents big problems.
Structure assignments in C know nothing about the internals of the struct, it's just a handy wrapper around
In C++, we would use a proper copy constructor to replicate what the pointer pointed to rather than just making a copy of the pointer.
Well it's fine as far as the syntax is concerned.
However, the pointer inside the structure presents big problems.
C++ Syntax (Toggle Plain Text)
fn1.y = malloc( 10 * sizeof *fn1.y ); fn2 = fn1; free( fn1.y ); // fn2.y is now a dangling pointer.
Structure assignments in C know nothing about the internals of the struct, it's just a handy wrapper around
memmove( &fn2, &fn1, sizeof fn2 );In C++, we would use a proper copy constructor to replicate what the pointer pointed to rather than just making a copy of the pointer.
![]() |
Similar Threads
- Little Simple array question :) (C)
- Simple array question (C++)
- Object-Oriented Programming (C++)
- Structure Help Please (C++)
- simple msn question (Windows Software)
- Simple (I think) Code Question (PHP)
Other Threads in the C++ Forum
- Previous Thread: Array and Loop for Paycheck
- Next Thread: Errors While Compiling
Views: 1085 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






