Thread: Rusty on strings and struct. need help!

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    8

    Rusty on strings and struct. need help!

    So im making this program where i have to manage passangers and stations with a few variables.

    Im now making the passanger.c i need to make 3 functions: 1 to create a passanger 1 destroy and another read points.

    Im kinda rusty on strings:
    passanger criaPassanger("how do i insert the strings here" int pts,int ue) and in the function. thanks


    Code:
    #include "passanger.h"
    #include <stdlib.h>
    
    typedef struct _passanger{
    	char Name[40];
    	char Email[40];
    	int points;
    	int last station;
    }infoPassanger, *apPassanger;
    
    passanger createPassanger(               int pts,int ue){
    	apPassanger pas;
    	pas=(apPassanger) malloc(sizeof(infoPassanger));
    	
    	
    	pas->points=pts;
    	pas->laststation=ue;
    	return (passanger)pas;
    }
    
    void destroyPassenger(passenger p){
    	free(p);
    
    int readPoints(passanger p){
    	apPassanger p2;
    	p2 = (apPassanger)p;
    	return(p2->points);
    Last edited by brigas; 03-25-2012 at 07:40 AM.

  2. #2
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    Seriously nobody can help me with this? i thought this would be something easy i was missing.
    can i do it like this?

    passanger createPassanger( char nm[], char mail[], int pts,int ue){
    apPassanger pas;
    pas=(apPassanger) malloc(sizeof(infoPassanger));
    pas->char nm[]= char name[40];
    pas->char mail[]= char email[40];

    pas->points=pts;
    pas->laststation=ue;
    return (passanger)pas;

    or something like this:

    passanger createPassanger( char *name, char *mail, int pts,int ue){
    apPassanger pas;
    pas=(apPassanger) malloc(sizeof(infoPassanger));
    pas->char *name= char name[40];
    pas->char *mail= char email[40];

    pas->points=pts;
    pas->laststation=ue;
    return (passanger)pas;
    Last edited by brigas; 03-25-2012 at 09:05 AM.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    It's often confusing to do typedefs the way you have, because masking arrays and pointers under a textual name hides the true type. Only use one typedef for a non-pointer and non-array type, and then re-write the memory management part.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Look through your books and notes. Have you ever seen either of these constructs?
    Code:
    pas->char nm[]= char name[40];
    Code:
    pas->char *name = char name[40];
    If you're rusty on strings and structs, then go read the tutorials on strings and structures!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rusty old programmer seeks help for small modification
    By jamesvdb in forum Projects and Job Recruitment
    Replies: 9
    Last Post: 01-16-2012, 06:45 PM
  2. Replies: 23
    Last Post: 05-11-2008, 07:12 AM
  3. A bit rusty.
    By VegasSte in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-20-2004, 08:11 PM
  4. I'm rusty and I need help
    By phay in forum C++ Programming
    Replies: 6
    Last Post: 08-12-2003, 10:12 PM
  5. A little Rusty (Need help with random)
    By Twig in forum C Programming
    Replies: 1
    Last Post: 07-19-2002, 04:42 AM