Thread: Array of pointers in c

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    1

    Question Array of pointers in c

    Feb 4, 2002

    Hai,
    My objective is too read a file with a sereis of letters, where each letter denotes a list of variabled such as atom_no , res_name etc.
    i have used an array of pointers(file _record) to which points to each instance of the atom_record.
    i get an error in line 36 a=(struct atom_record *)malloc(sizeof(struct atom_record));

    which says 'incompatible types in assignment'

    help me solve it. i have attached the program below.


    #include<stdio.h>
    #include<stdlib.h>

    struct atom_record
    {
    int atom_number;
    char* atom_name1[3];
    char* atom_name2[3];
    char* atom_name3[3];
    char* atom_name4[3];
    char* atom_name5[4];
    int residue_number;
    char* residue_name[4];
    }a;



    struct file_record

    {
    struct atom_record **atoms;
    }*f;


    main()
    {
    char c;int atom_counter=0;
    FILE *f1;

    f1=fopen("pdb1de6.fasta","r");


    while((c=getc(f1))!=EOF)
    {
    a=(struct atom_record *)malloc(sizeof(struct atom_record));
    f->atoms[atom_counter]=&a;

    switch(c)
    {
    case 'G':
    a.residue_name[4]="GLY";
    atom_counter++;
    break;

    case 'A':
    a.residue_name[4]="ALA";
    atom_counter++;
    break;


    case 'V':
    a.residue_name[4]="VAL";
    atom_counter++;
    break;

    case 'L':
    a.residue_name[4]="LEU";
    atom_counter++;
    break;

    case 'I':
    a.residue_name[4]="ILE";
    atom_counter++;
    break;

    case 'S':
    a.residue_name[4]="SER";
    atom_counter++;
    break;

    case 'T':
    a.residue_name[4] ="THR";
    atom_counter++;
    break;

    case 'C':
    a.residue_name[4]="CYS";
    atom_counter++;
    break;

    case 'M':
    a.residue_name[4]="MET"; atom_counter++;
    break;

    case 'P':
    a.residue_name[4]="PRO"; atom_counter++;
    break;


    case 'D':
    a.residue_name[4]="ASP"; atom_counter++;
    break;


    case 'N':
    a.residue_name[4]="ASN";
    break;

    case 'E':
    a.residue_name[4]="GLU"; atom_counter++;
    break;

    case 'Q':
    a.residue_name[4]="GLN";atom_counter++;
    break;

    case 'K':
    a.residue_name[4]="LYS"; atom_counter++;
    break;


    case 'R':
    a.residue_name[4]="ARG"; atom_counter++;
    break;


    case 'H':
    a.residue_name[4]="HIS"; atom_counter++;
    break;

    case 'F':
    a.residue_name[4]="PHE"; atom_counter++;
    break;

    case 'Y':
    a.residue_name[4]="TYR";atom_counter++;
    break;

    case 'W':
    a.residue_name[4]="TRP"; atom_counter++;
    break;

    case '>':
    while((c=getc(f1))!='\n')
    continue;1
    break;

    case '\n':
    break;

    default:
    break;

    }

    a.atom_number=atom_counter;
    a.atom_name1[3]="N";
    a.atom_name2[3]="CA";
    a.atom_name3[3]="C";
    a.atom_name4[3]="O";
    a.atom_name5[4]="CEN";
    a.residue_number;
    write a.residue_name[4];
    }
    fclose(f1);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well, there's a whole bunch of memory allocation problems, and some other stuff to fix

    > struct atom_record
    > {
    > int atom_number;
    > char* atom_name1[3];
    > char* atom_name2[3];
    > char* atom_name3[3];
    > char* atom_name4[3];
    > char* atom_name5[4];
    > int residue_number;
    > char* residue_name[4];
    > }a;

    Ok, several things wrong here (judging from the rest of the code).
    1. Each element doesn't need to be an array of pointers, just an array. You seem to be trying to copy in a string.
    2. The 'a' at the end creates a variable of this type (struct atom_record), but this is a pointless variable, since you're going to be allocating them
    3. name1 to name5 suggests an array

    So, it should be
    Code:
    struct atom_record {
      int  atom_number;
      char atom_name[5][3];
      int  residue_number;
      char residue_name[4];
    };
    > struct file_record
    > {
    > struct atom_record **atoms;
    > }*f;
    Try
    Code:
    struct file_record {
      struct atom_record **atoms;
    }f;
    You need a real variable at the top of the data structure to hang everything off.

    > while((c=getc(f1))!=EOF)
    This is fine, but you'll need to make 'c' an int, not a char, otherwise you'll never match EOF


    > a=(struct atom_record *)malloc(sizeof(struct atom_record));
    > f->atoms[atom_counter]=&a
    This is the real problem in terms of memory allocation
    I think you wanted to write it like this
    Code:
    f.atoms[atom_counter] = (struct  atom_record *)malloc(sizeof(struct atom_record));
    But this also fails, since f.atoms is itself just an uninitialised pointer.
    If you know the max value of atom_count, then this fixes this problem

    [code]
    struct file_record {
    struct atom_record *atoms[MAX_ATOM_COUNT];
    }f;

    Finally,
    > a.residue_name[4]="GLY";
    This would become
    strcpy( f.atoms[atom_counter].residue_name, "GLY" );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  2. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. array of pointers to struct array
    By eth0 in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 06:43 PM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 07:13 AM