spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Access struct member from pointer


As said by barak manos, your problem lies in init. C pass parameters by value so let's imagine :

  • you set a pointer to pile to NULL (pile *p = NULL;)
  • you pass it to init (init(p);)
  • in init you alloc a pile and affect is to the local copy of p
  • on return from init, p is still NULL and you have a memory leak since you have no longer any pointer to the allocated pile

That's the reason why you should write :

void init(int max, pile ** p) {
    pile *lp = *p = (pile *)malloc(sizeof(pile));
    if(*p){
        lp->nbElemPresent = 0;
        lp->maxElem       = max;
        lp->tete          = (data *)malloc(max
* sizeof(data));
    }
}

Now you use :

pile *p;
init(&p);

and on return p points to the allocated pile.


Categories : C

Related to : Access struct member from pointer
Do GCC/Clang allow to access static member through null pointer?
I can find this neither in the list of gcc's C++ extensions nor in the corresponding list for clang. So I would say that you have no guarantee that this works for either compiler.

Categories : C++
print member of struct of a vector
Boost Adaptor adapts ranges, not single iterators. Similarily, the boost::copy algorithm expects two arguments, where the first one is a range, and the second an output iterator. Having said that, the correct syntax for extracting a single data member from a range, and copying it to an output iterator is: boost::copy(pntVec | boost::adaptors::transformed(bind(&pnt::_type, _1)) , std

Categories : C++
C - Help printing a member of a returned Struct
Your struct busRoute should be defined in BusRoute.h, not BusRoute.c, if you want to use it in main.c. And you do want to use it in main.c, if your getBusRouteInfo() function returns one. Without main.c being able to see this definition, it doesn't know that struct busRoute even has a member named busRouteNumber, let alone how to get at it, so that's why compilation fails. Your other option is to

Categories : C
Initializing pointer arrays to NULL within a struct
An object of any type can be initialized to zero: struct A myA = { 0 }; This initializes the first scalar member or element (defined recursively for nested arrays, structures, and unions) to 0 converted to the appropriate type, which is a null pointer value if it's a pointer. All remaining members or elements are also initialized to zero. Some compilers may warn you about the missing initializ

Categories : C
returning bool from member variable pointer
hasStarted() is fine: in C++, the pointer will be automatically converted to bool on a zero/not-zero basis.† Some people prefer to write return (p != nullptr) or return static_cast<bool>(p) instead, in order to be really explicit about intent, but I'm not terribly fussy about that in cases like this. † However, the behaviour is implementation-defined in C, so do not rely on i

Categories : C++
Recently Add
Rewrite of IDA decompiled function
Computing the average of grades in C
passing structures to functions
Use semaphores for handling sockets in C
How to search for a string pattern inside html, coding in C?
sprintf invalid format '%d'
Why can't this c program run correctly?
Low Pass filter in C
Child process does not print anything
C: datatypes. sqrt function working with int why?
How to implement Serial Port Profile Link Command used in ConnectBlue Bluetooth Module?
Rerunning cancelled pthread
How to print out a string that is pointed to by a char pointer?
Can unverified scanf call cause an undefined behavior?
nested structures in C
Portable Makevars for R package using C, GSL and OpenMP with help of Rcpp
program doesn't run(getting RUN FINISHED Segmentation fault core dumped)
Finding the sign of a Lua number, from C
Width of symbols created by gcc's objectcopy
Strange behavior when using free in c program
Can't open image C
Strange C code - dynamic arrays?
C program printing weird characters
Fault in decimal to binary in c
How to detect broken pipe on input?
How can I ivestigate use of the register keyword in C?
File scaning with fscanf
why gcc generates "leal -8240(%ebp), %eax; cmpl $1, %eax; ja XXX" instead of jmp instruction?
Wierd Problems With My Own Custom Written Word Search Algorithm
Trouble with making a shell in C
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.