Thread: Typedef Problem

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    Typedef Problem

    I have created my own structure using typedef but when I go to store values in a created one, I am going incompatible type errors

    Code:
    typedef struct{
    	FILE *fp;
    	char name[50];
    	char line[100];
    	int timestart, timetotal;
    } process;
    
    void newprocess(char n[]){
    	process *new = malloc(sizeof(process));
    	new->name = n;
    When I try to compile this I get error: incompatible types in assignment.

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    You will have to use strncpy() for what you're trying to do, because you can only assign a value to an entire array at declaration. Past that, you have to set values of individual array elements.
    Code:
    char full_name[50] = "Jane Doe";    // Fine.
    
    . . .
    
    char full_name[50];
    full_name = "Jane Doe";    // Invalid.
    Last edited by msh; 10-21-2010 at 01:39 AM.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    1
    you know what this function is not used like this "process *new = malloc(sizeof(process))"

    this is it , "void* malloc()"
    so you should define a structure point to changes its type!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by 2008hiroki
    you know what this function is not used like this "process *new = malloc(sizeof(process))"

    this is it , "void* malloc()"
    so you should define a structure point to changes its type!
    You are right to say that there is a problem with that line as well, but other than that I do not understand what you are trying to say about "this is it" and "a structure point to changes its type". That line should have been:
    Code:
    process *new = malloc(sizeof(*process));
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Help...typedef struct...confusing...
    By darkchild in forum C Programming
    Replies: 1
    Last Post: 01-23-2007, 08:03 AM
  3. typedef problem
    By dr$brown in forum C Programming
    Replies: 6
    Last Post: 12-25-2004, 12:27 PM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM