Using malloc in C. Please help me.

This is a discussion on Using malloc in C. Please help me. within the C Programming forums, part of the General Programming Boards category; I have a code and i must complete it, make b[i]==a[i]. Code: #include <stdio.h> #include <stdlib.h> #define N 5 int ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    6

    Using malloc in C. Please help me.

    I have a code and i must complete it, make b[i]==a[i].
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define N 5
    
    int a[N]={80,75,50,90,45};
    
    int main(int argc, char **argv) {
    
    int i;
    
    int *b;
    
    b=(int *)malloc(sizeof(int)*N);
    
    if(b==NULL) {
    fprintf(stderr,"malloc error\n");
    exit(1);
    }
    
    for(i=0;i<N;i++) {
     _________________					
         }
     
     for(i=0;i<N;i++) {
         printf("%d\n",b[i]);
     }
     
     free(b);
     }
    I must fill in the underline. And i think i must only write:
    Code:
    for(i=0;i<N;i++) {
          b[i]=a[i];
    }
    Is that correct?
    And when i try to find some example about malloc from internet, i have this code:
    Code:
    typedef struct data_type {
      int age;
      char name[20];
    } data;
    
    data *bob;
    bob = (data*) malloc( sizeof(data) );
    if( bob != NULL ) {
      bob->age = 22;
      strcpy( bob->name, "Robert" );
      printf( "%s is %d years old\n", bob->name, bob->age );
    }
    free( bob );
    I don't understand why they must write this sign "->"
    Code:
    bob->age = 22;
      strcpy( bob->name, "Robert" );
    thanks for your help.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> And i think i must only write:

    That is correct.

    >> I don't understand why they must write this sign "->"

    Because you're working with a pointer. That's just how it's done. You could of course dereference the pointer, in order to be able to access the members as you would a non-pointer, eg:

    Code:
    (*bob).age = 22;
    >> (int *)malloc(sizeof(int)*N);

    You don't need to cast the result. In C, you can assign any kind of pointer directly to a void*.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    5

    Lightbulb pointer variable

    here I gave one example for this .
    :insert
    Code:
    struct student {
        int age;
        char name[25];
          };
    
    struct student ss;
    now the structure variable is "ss" it is not a pointer. I can access the structure member by the following way.
    :insert
    Code:
    ss.age=22;
    strcpy(ss.name,"sugumar");
    struct student ........;
    now the structure variable is pointer variable. I can't access the structure member by the following way. Ss.age=22;

    if I want to access the structure member I need to use the "->" symbol.
    :insert
    Code:
    Ss->age=22;
    strcpy(Ss->name,"dhivya Lakshmi");
    Last edited by sugumar.tr; 07-25-2009 at 03:15 AM. Reason: making differencd between text and code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 04:46 PM
  2. the basics of malloc
    By nakedBallerina in forum C Programming
    Replies: 21
    Last Post: 05-20-2008, 03:32 AM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 06:37 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21