Thread: modifying struct values

  1. #1
    Registered User
    Join Date
    May 2007

    Unhappy modifying struct values

    #include <stdio.h>
    typedef struct{
    	int size;
    void setSize(FileADT *file){
    	printf("1->%d",file->size);/*prints the file size correctly*/
    int main(void){
    	FileADT *a;
    	a = (FileADT*)malloc(sizeof(FileADT));
    	printf("2->%d",a->size);   /*DOESNT WORK.PROGRAM CRASHES*/

    hi,I got a struct.I want to be able to modify the valueS of the struct.
    Inside the set size function,i set the value, and it correctly prints the size in the function.
    but back in the main function it doesnt print the file size.

    I wonder what mistake i am making


  2. #2
    Registered User
    Join Date
    Aug 2007
    Theres your mistake..

    setSize( a);
    setSize (&a) passes in the address of a pointer ie **a

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > a = (FileADT*)malloc(sizeof(FileADT));
    Remove the cast, recompile and make a note of the error message.
    Then add
    #include <stdlib.h>
    and recompile, noting that the error message has gone away.

    See the FAQ on why casting malloc in C is a bad idea.

    > setSize(&a);
    And your compiler didn't complain about this?
    Or did you just ignore it and carried on anyway?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM