LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-23-2012, 03:15 AM   #16
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled

Quote:
Originally Posted by dwhitney67 View Post
strcmp() compares two strings, or array of chars which are null-terminated. The 'cell_phone' function parameter is declared as a pointer to a char, which presumably points to a string; a pointer to a char and a char are not the same.

I cannot comment on whether the usage of ppension->cell_phone[i] is correct or not because I have not seen how it is declared. As for the variable cell_phone, you should not be de-referencing it, for this would yield a single character. Try something like this:
Code:
if(!(strcmp(ppension->cell_phone[i], cell_phone)))    /* Note that I removed the * from cell_phone */
hi

i cant understand you:

now you insert a pointer (type char*) to strcmp function...however from what i know, strcmp must recieve a char and now a char pointer?

thanks in advance
 
Old 04-23-2012, 03:37 AM   #17
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
hi i have one more question about struct:

if i want to implement a struct in struct:

this is what i wrote:

the pension1.h file:

Code:
/* pension.h - pension header file */

#ifndef _PENSION_H_
#define _PENSION_H_

/* constants' definitions */
#define MAX_DOGS 500 /* maximum number of dogs of the same type */
#define MAX_DOGS_PER_PERSON 5 /* maximum number of dogs that one person can have */
#define CELL_PHONE_LENGTH 12 /* the length of a cellphone's string including NULL in the end */
#define DATE_LENGTH 11 /* the length of a date's string including NULL in the end */
#define TYPE1_COST 70  /* cost for a male dog of type 1 */
#define TYPE2_COST 1000  /* monthly cost for a male dog of type 2 */
#define TYPE3_COST 100  /* cost per day for a male dog of type 3 */
#define FEMALE_EXTRA_COST 5 /* % of extra cost for female dogs */
#define DISCOUNT_PER_DOG 10 /* % of discount per dog for each dog when a person has multiple dogs */

/* types' definitions */

/* define your structs and their types here.  */



typedef struct dogs_type1 {
    int top1;
    int* dog_id1;
    char* dog_name1;
    char* dog_gender1;
    char* first_name1;
    char* family_name1;
    char* cell_phone1;
    }type1;

typedef struct dogs_type2 {
    int top2;
    int* dog_id2;
    char* dog_name2;
    char* dog_gender2;
    char* first_name2;
    char* family_name2;
    char* cell_phone2;
    char* date2;
    }type2;

typedef struct dogs_type3 {
    int top3;
    int* dog_id3;
    char* dog_name3;
    char* dog_gender3;
    char* first_name3;
    char* family_name3;
    char* cell_phone3;
    char* date3;
    int* days_num3;
    }type3;


typedef struct _PENSION {
    type1 dogs1;
    type2 dogs2;
    type3 dogs3;
    }PENSION;
typedef enum _BOOL {FALSE=0 , TRUE} BOOL;

/* interface functions */
PENSION* CreatePension();
BOOL AddDog(PENSION* ppension, int dog_id, char* dog_name, char dog_gender, char* first_name, char* family_name, char* cell_phone, char* date, int days_num);
BOOL RemoveDog(PENSION* ppension, int dog_id);
BOOL PrintDetails(PENSION* ppension, char* cell_phone);
void PrintBills(PENSION* ppension);
void FreePension(PENSION* ppension);

#endif /*_PENSION_H_*/
and this is the pension1.c file:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pension1.h"

PENSION* CreatePension() {
    PENSION* arrays_pointer;
    arrays_pointer=(PENSION*) malloc(sizeof(PENSION*));
    type1* dogs1;
    type2* dogs2;
    type3* dogs3;
    dogs1=(type1*) malloc(sizeof(type1*));
    dogs2=(type2*) malloc(sizeof(type2*));
    dogs3=(type3*) malloc(sizeof(type3*));


    if((arrays_pointer == NULL) || (dogs1 == NULL) || (dogs2 == NULL)|| (dogs3 == NULL)){
        exit(1);
    }
    arrays_pointer->dogs1->top1=0;
    arrays_pointer->dogs1->dog_id1=(int*) malloc(MAX_DOGS);
    arrays_pointer->dogs1->dog_name1=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs1->dog_gender1=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs1->first_name1=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs1->family_name1=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs1->cell_phone1=(char*) malloc(MAX_DOGS);


    arrays_pointer->dogs2->top2=0;
    arrays_pointer->dogs2->dog_id2=(int*) malloc(MAX_DOGS);
    arrays_pointer->dogs2->dog_name2=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs2->dog_gender2=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs2->first_name2=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs2->family_name2=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs2->cell_phone2=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs2->date2=(char*) malloc(MAX_DOGS);


    arrays_pointer->dogs3->top3=0;
    arrays_pointer->dogs3->dog_id3=(int*) malloc(MAX_DOGS);
    arrays_pointer->dogs3->dog_name3=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs3->dog_gender3=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs3->first_name3=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs3->family_name3=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs3->cell_phone3=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs3->date3=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs3->days_num3=(int*) malloc(MAX_DOGS);


    return arrays_pointer;

}
however i have alot of errors(24 errors) :

Code:
||In function `CreatePension':||20|error: invalid type argument of `->'|
what is the problem?is it a wrong imlementation fo struct in struct?what is the right syntax to do it?

**** in general, how cn i define typedef struct inside typedef struct?
thanks alot

Last edited by ofer4; 04-23-2012 at 03:55 AM.
 
Old 04-23-2012, 06:17 AM   #18
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
strcmp() compares two strings, not two char values. If you referenced any book or online tutorial that indicated that two chars are compared, then you should dispose of it.

As for your next query, it would seem that you are developing redundant code. If you have two or more structures that will contain the same data, then it would be wiser to define a structure that defines the common data. For example:
Code:
struct Person
{
    char firstName[30];
    char lastName[30];
};

struct Address
{
    char street[80];
    char city[30];
    char country[30];
};

struct Student
{
    struct Person  student;
    struct Address address;
    float          gradePointAverage;
};

struct Employee
{
    struct Person  employee;
    struct Address address;
    float          salary;
};
As for your obsessiveness with the usage of malloc(), it is definitely not required in all cases. As my example demonstrates above, it is possible to make educated guesses as to what the maximum size that a string will require. You are allocating 500 bytes for every member within a structure, which seems wasteful.

Taking the code I've shown above, here would be a typical example of its application:
Code:
struct Employee* emp = malloc(sizeof(struct Employee));

printf("Enter employee first name: ");
fgets(emp->employee.firstName, sizeof(emp->employee.firstName), stdin);

printf("Enter employee last name: ");
fgets(emp->employee.lastName, sizeof(emp->employee.lastName), stdin);

...

char* input = malloc(50);
printf("Enter employee salary: ");
fgets(input, 50, stdin);

int result = sscanf(input, "%f", &emp->salary);

if (result != 1)
{
    /* error with input */
}

...
Now, if I want to compare to see if a Student is also an Employee, I could do something like:
Code:
if (strcmp(stud->student.firstName, emp->employee.firstName) == 0 &&
    strcmp(stud->student.lastName, emp->employee.lastName) == 0)
{
    /* names match */
}
To conclude this response, if any of the concepts above are not clear to you, I would suggest that you revisit a good tutorial. Someone on this forum recently floated this link to a supposedly good tutorial: http://beej.us/guide/bgc/output/print/bgc_A4.pdf
I have not looked at it myself, but Beej is considered a respected author for a similar tutorial for Socket/Network programming.
 
Old 04-23-2012, 07:20 AM   #19
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
hi

i did exactly what you explain to me:

pension1.c:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pension1.h"

PENSION* CreatePension() {

    PENSION* arrays_pointer;
    arrays_pointer=(PENSION*) malloc(sizeof(PENSION*));


    if((arrays_pointer == NULL) ){
        exit(1);
    }
    arrays_pointer.dogs1.top1=0;
    arrays_pointer.dogs1.dog_id1=(int*) malloc(MAX_DOGS);
    arrays_pointer.dogs1.dog_name1=(char*) malloc(MAX_DOGS);
    arrays_pointer.dogs1.dog_gender1=(char*) malloc(MAX_DOGS);
    arrays_pointer.dogs1.first_name1=(char*) malloc(MAX_DOGS);
    arrays_pointer.dogs1.family_name1=(char*) malloc(MAX_DOGS);
    arrays_pointer.dogs1.cell_phone1=(char*) malloc(MAX_DOGS);


    arrays_pointer->dogs2->top2=0;
    arrays_pointer->dogs2->dog_id2=(int*) malloc(MAX_DOGS);
    arrays_pointer->dogs2->dog_name2=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs2->dog_gender2=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs2->first_name2=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs2->family_name2=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs2->cell_phone2=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs2->date2=(char*) malloc(MAX_DOGS);


    arrays_pointer->dogs3->top3=0;
    arrays_pointer->dogs3->dog_id3=(int*) malloc(MAX_DOGS);
    arrays_pointer->dogs3->dog_name3=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs3->dog_gender3=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs3->first_name3=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs3->family_name3=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs3->cell_phone3=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs3->date3=(char*) malloc(MAX_DOGS);
    arrays_pointer->dogs3->days_num3=(int*) malloc(MAX_DOGS);


    return arrays_pointer;

}
pension1.h:

Code:
/* pension.h - pension header file */

#ifndef _PENSION_H_
#define _PENSION_H_

/* constants' definitions */
#define MAX_DOGS 500 /* maximum number of dogs of the same type */
#define MAX_DOGS_PER_PERSON 5 /* maximum number of dogs that one person can have */
#define CELL_PHONE_LENGTH 12 /* the length of a cellphone's string including NULL in the end */
#define DATE_LENGTH 11 /* the length of a date's string including NULL in the end */
#define TYPE1_COST 70  /* cost for a male dog of type 1 */
#define TYPE2_COST 1000  /* monthly cost for a male dog of type 2 */
#define TYPE3_COST 100  /* cost per day for a male dog of type 3 */
#define FEMALE_EXTRA_COST 5 /* % of extra cost for female dogs */
#define DISCOUNT_PER_DOG 10 /* % of discount per dog for each dog when a person has multiple dogs */

/* types' definitions */

/* define your structs and their types here.  */
typedef struct dogs_type1 {
    int top1;
    int* dog_id1;
    char* dog_name1;
    char* dog_gender1;
    char* first_name1;
    char* family_name1;
    char* cell_phone1;
    }type1;

typedef struct dogs_type2 {
    int top2;
    int* dog_id2;
    char* dog_name2;
    char* dog_gender2;
    char* first_name2;
    char* family_name2;
    char* cell_phone2;
    char* date2;
    }type2;

typedef struct dogs_type3 {
    int top3;
    int* dog_id3;
    char* dog_name3;
    char* dog_gender3;
    char* first_name3;
    char* family_name3;
    char* cell_phone3;
    char* date3;
    int* days_num3;
    }type3;

typedef struct _PENSION {

    type1* dogs1;
    type2* dogs2;
    type3* dogs3;

    }PENSION;
typedef enum _BOOL {FALSE=0 , TRUE} BOOL;

/* interface functions */
PENSION* CreatePension();
BOOL AddDog(PENSION* ppension, int dog_id, char* dog_name, char dog_gender, char* first_name, char* family_name, char* cell_phone, char* date, int days_num);
BOOL RemoveDog(PENSION* ppension, int dog_id);
BOOL PrintDetails(PENSION* ppension, char* cell_phone);
void PrintBills(PENSION* ppension);
void FreePension(PENSION* ppension);

#endif /*_PENSION_H_*/
and it continue to show me an error messagefor all the structs, this is just an example):

Code:
error: request for member `dogs1' in something not a structure or union|
i read alot of material about it over google and i didnt succssed to figure it out...can someone help me please?

Last edited by ofer4; 04-23-2012 at 07:21 AM.
 
Old 04-23-2012, 08:41 AM   #20
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Lines like
Code:
arrays_pointer.dogs1.top1=0;
should read
Code:
arrays_pointer->dogs1->top1=0;
There is a rule: if you have structure X then use X.member to acces its members; if you have a pointer to structure then use either (*X).member or X->member.

As dwhitney67 already said, your code looks redundant. If you want an array of structures (DOGS, whatever it means) then create an array of structures, not structure of arrays, as you seem to be trying to do.

Last edited by firstfire; 04-23-2012 at 08:57 AM.
 
Old 04-23-2012, 03:47 PM   #21
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by firstfire View Post
Hi.

Lines like
Code:
arrays_pointer.dogs1.top1=0;
should read
Code:
arrays_pointer->dogs1->top1=0;
There is a rule: if you have structure X then use X.member to acces its members; if you have a pointer to structure then use either (*X).member or X->member.

As dwhitney67 already said, your code looks redundant. If you want an array of structures (DOGS, whatever it means) then create an array of structures, not structure of arrays, as you seem to be trying to do.

tnx alot...

i have one more problem:

i wrote this:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pension1.h"

#define MAX_LINE_SIZE 255

int main() {
  char szLine[MAX_LINE_SIZE];
  char* delimiters = " \t\n";
  char* command;
  char* str_dog_id;
  int dog_id;
  BOOL result;
  char* kelet[10];
  int i=0,a,b,c,d,j,days;
  PENSION* ppension = CreatePension();

  while (fgets(szLine, MAX_LINE_SIZE, stdin)) {
	  memset(kelet,66,10*sizeof(char*));
        kelet[0] = strtok(szLine, delimiters);
		while( i<10 ) {
			i++;
			kelet[i] = strtok( NULL, delimiters );
		}
when i get first time to fgets and i enter in the command windows this:

"Add 1234 Tobby M Noa Levi 052-1234567"

and it works correctly:

*kelet[0]='Add'
*kelet[1]='1234'

however when i get to the fgets in the second time and enter again:


"Add 1234 Tobby M Noa Levi 052-1234567"

the kelet array does not consist the correct strings:

kelet[0]='Add'

and every other kelet[i>0]=NULL

i really dont know why the kelet[0] is the only one that consist the correct string and the other variables are NULL

please someone can help me?

i spent alot of houres to understand where am i wrong and i didnt find anything

thanks in advance!
 
Old 04-23-2012, 04:07 PM   #22
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
You have declared kelet to be an array of 10 pointers to char.

I'm not sure why you want to set the kelet array values to a value of 66; typically one would set the values to NULL (or 0). Also, you should not be seeking exactly 10 fields from the string that was entered, but instead looking to ensure that you do not end up with a NULL field.

By the way, the input statement that you provided only contains 7 fields, not 10.

Consider this code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    const char* delimiters = " \t\n";

    char* kelet[10];
    char  input[255];

    while (fgets(input, sizeof(input), stdin))    /* use ctrl-d to exit while loop */
    {
        char* token = strtok(input, delimiters);
        int   i = 0;

        memset(kelet, 0, sizeof(kelet));

        while (token && i < 10)
        {
            kelet[i++] = token;

            token = strtok(NULL, delimiters);
        }

        /* verify parsing of input */
        for (int j = 0; j < i; ++j)
        {
            printf("token %d: %s\n", j, kelet[j]);
        }
    }

    return 0;
}
 
Old 04-23-2012, 04:26 PM   #23
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
thanks alot now it works...
 
Old 04-24-2012, 12:47 AM   #24
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
hi i have one more problem:

i wrote the header file pension.h:

Code:
/* pension.h - pension header file */

#ifndef _PENSION_H_
#define _PENSION_H_

/* constants' definitions */
#define MAX_DOGS 500 /* maximum number of dogs of the same type */
#define MAX_DOGS_PER_PERSON 5 /* maximum number of dogs that one person can have */
#define CELL_PHONE_LENGTH 12 /* the length of a cellphone's string including NULL in the end */
#define DATE_LENGTH 11 /* the length of a date's string including NULL in the end */
#define TYPE1_COST 70  /* cost for a male dog of type 1 */
#define TYPE2_COST 1000  /* monthly cost for a male dog of type 2 */
#define TYPE3_COST 100  /* cost per day for a male dog of type 3 */
#define FEMALE_EXTRA_COST 5 /* % of extra cost for female dogs */
#define DISCOUNT_PER_DOG 10 /* % of discount per dog for each dog when a person has multiple dogs */

/* types' definitions */

/* define your structs and their types here.  */





typedef struct _PENSION {

    int top1;
    int* dog_id1;
    char* dog_name1;
    char* dog_gender1;
    char* first_name1;
    char* family_name1;
    char* cell_phone1;

    int top2;
    int* dog_id2;
    char* dog_name2;
    char* dog_gender2;
    char* first_name2;
    char* family_name2;
    char* cell_phone2;
    char* date2;

    int top3;
    int* dog_id3;
    char* dog_name3;
    char* dog_gender3;
    char* first_name3;
    char* family_name3;
    char* cell_phone3;
    char* date3;
    int* days_num3;


    }PENSION;
typedef enum _BOOL {FALSE=0 , TRUE} BOOL;

/* interface functions */
PENSION* CreatePension();
BOOL AddDog(PENSION* ppension, int dog_id, char* dog_name, char dog_gender, char* first_name, char* family_name, char* cell_phone, char* date, int days_num);
BOOL RemoveDog(PENSION* ppension, int dog_id);
BOOL PrintDetails(PENSION* ppension, char* cell_phone);
void PrintBills(PENSION* ppension);
void FreePension(PENSION* ppension);

#endif /*_PENSION_H_*/
and the function :

Code:
BOOL AddDog(PENSION* ppension, int dog_id, char* dog_name, char dog_gender, char* first_name, char* family_name, char* cell_phone, char* date, int days_num) {

    int i,a;

	char b;
/* type 1 */
    if((date == NULL) && (days_num==-1)) {

        for(i=0;i<=ppension->top1;i++)
        {
			a=(ppension->dog_id1[i]);
			b=ppension->cell_phone1[i];
			
            if((ppension->dog_id1[i]==dog_id) && ((!strcmp(cell_phone,&ppension->cell_phone1[i])))) {
                return FALSE;
            }
        }

        ppension->dog_id1[ppension->top1]=dog_id;
        ppension->dog_name1[ppension->top1]=dog_name;
        ppension->dog_gender1[ppension->top1]=dog_gender;
        ppension->first_name1[ppension->top1]=first_name;
        ppension->family_name1[ppension->top1]=family_name;
        ppension->cell_phone1[ppension->top1]=cell_phone;

        ppension->top1++;

        return TRUE;
    }
however this function didnt work correctly for me...

if "dog_name" consist "Tobby" word for example ,ppension->dog_name1[ppension->top1] consist only "T" insead of "Tobby"

i want that ppension->dog_name1[i ] which i goes between 0 to MAX_DOGS will contain the word that it will receive from the function (main.c)

p.s this is the program (main.c) which call to Add Dog function:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pension1.h"

#define MAX_LINE_SIZE 255

int main() {
  char szLine[MAX_LINE_SIZE];
  char* delimiters = " \t\n";
  char* command;
  char* str_dog_id;
  int dog_id;
  BOOL result;
  char* kelet[10];
  char  input[255];
  int i=0,a,b,c,d,j,id;
  PENSION* ppension = CreatePension();

  while (fgets(input, sizeof(input), stdin))    /* use ctrl-d to exit while loop */
    {
        char* token = strtok(input, delimiters);
        int   i = 0;

        memset(kelet, 0, sizeof(kelet));

        while (token && i < 10)
        {
            kelet[i++] = token;

            token = strtok(NULL, delimiters);
        }
  

			if (strcmp(kelet[0], "Add") == 0) {

				id=atoi(kelet[1]);
				if((kelet[7]==(char*)NULL) && (kelet[8]==(char*)NULL))
				{
					result=AddDog(ppension,id,kelet[2],*kelet[3],kelet[4],kelet[5],kelet[6],kelet[7],-1);
				}
				else if((kelet[8]==(char*)NULL))
				{
					result=AddDog(ppension,atoi(kelet[1]),kelet[2],*kelet[3],kelet[4],kelet[5],kelet[6],kelet[7],-1);
				}
				else
				{
					result=AddDog(ppension,atoi(kelet[1]),kelet[2],*kelet[3],kelet[4],kelet[5],kelet[6],kelet[7],*kelet[8]);
				}

what is the problem?

i need an advice

thanks alot

Last edited by ofer4; 04-24-2012 at 01:09 AM.
 
Old 04-24-2012, 05:00 AM   #25
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by ofer4 View Post
hi i have one more problem:
I was anticipating this...

Quote:
Originally Posted by ofer4 View Post
what is the problem?
This line of code, and the ones similar to it, has a bug:
Code:
ppension->dog_id1[ppension->top1]=dog_id;
It is ppension that is an array of pension structures; the dog_id1 is a mere pointer to a char, which presumably will point to a string of characters that are null terminated.

You need to reference the appropriate pension structure using something like, if indeed top1 is an index value to the array:
Code:
ppension[ppension->top1]->dog_id1 = <str ptr>
Let me explain why I used <str ptr> above; in your main() function you are obtaining individual string tokens using strtok(), however these are mere pieces of the array "input", which appears that you will be using over an over again. This will affect your data, with perhaps only the last entry being added to ppension being valid.

You are going to a) have to allocate duplicates of your strings (e.g. using strdup()), or b) declare your structure members (e.g. dog_id1) as an array of characters and not a pointer to characters. I advised you on option b earlier, but I presume you did not comprehend this advice. For option b), usage of strncpy() would be required.

If you have a book/tutorial, go back to read about strings, about copying strings, and then about pointers.


P.S. This could be simplified; you have 3 of the same variable... why is that?
Code:
typedef struct _PENSION {

    int top1;
    int* dog_id1;
    char* dog_name1;
    char* dog_gender1;
    char* first_name1;
    char* family_name1;
    char* cell_phone1;

    int top2;
    int* dog_id2;
    char* dog_name2;
    char* dog_gender2;
    char* first_name2;
    char* family_name2;
    char* cell_phone2;
    char* date2;

    int top3;
    int* dog_id3;
    char* dog_name3;
    char* dog_gender3;
    char* first_name3;
    char* family_name3;
    char* cell_phone3;
    char* date3;
    int* days_num3;

    }PENSION;

Last edited by dwhitney67; 04-24-2012 at 05:06 AM.
 
Old 04-24-2012, 05:49 AM   #26
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
hi

i tried option b..however it is impossible to declare dog_name1 as a character array becuase malloc must get a char*

and the second option:

Code:
ppension[ppension->top1]->dog_id1 = <str ptr>
is not what i mean to do...because now ppension will be an array and i want that ppension->dog_name1 will be an array of char (pointers to char)

maybe i didnt understand you?or someone else have another idea?

ps i have 3 of the same variable because i have 3 types of dogs..every type will have his own array

thanks in advance

Last edited by ofer4; 04-24-2012 at 06:06 AM.
 
Old 04-24-2012, 06:08 AM   #27
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by ofer4 View Post
hi

i tried option b..however it is impossible to declare dog_name1 as a character array becuase malloc must get a char*
Why must you insist on using malloc()?

A case where malloc() is not required:
Code:
char array[50];
A case where malloc() is required:
Code:
char* array = malloc(50);
Quote:
Originally Posted by ofer4 View Post
and the second option:

Code:
ppension[ppension->top1]->dog_id1 = <str ptr>
is not what i mean to do...because now ppension will be an array and i want that ppension->dog_name1 will be an array of char (pointers to char)
ppension is an array, albeit of only one element; refer back to your CreatePension() function:
Code:
PENSION* CreatePension() {

    PENSION* arrays_pointer;
    arrays_pointer=(PENSION*) malloc(sizeof(PENSION*));
...
I really suspect that you are getting confused between the difference between array and a pointer to an array (or other type). The following is legal code:
Code:
char array[50];           /* here, 50 contiguous bytes are reserved on the stack. */

char* ptr = array;        /* here, we setup a pointer to the first element of the array. */

char* ptr2 = &array[0];   /* this has the same effect as the statement above. */
More examples:
Code:
/* This is a mere definition of a structure; no memory is allocated for this structure at this time. */
struct Foo
{
    char name[50];
};

...

struct Foo myFoo;    /* here, space of the size of struct Foo is reserved on the stack. */

struct Foo* ptr = &myFoo;    /* here, a pointer is assigned to reference the location in memory for myFoo */

struct Foo* ptr2 = malloc(sizeof(struct Foo));   /* here, memory is allocated on the heap (not the stack!) */
Continuing, a string can be allocated, and accessed if it were an array:
Code:
char* str = malloc(50);   /* allocate 50 bytes on the heap */

str[0] = 'T';
str[1] = 'h';
str[2] = 'e';
str[3] = ' ';
str[4] = 'E';
str[5] = 'n';
str[6] = 'd';
str[7] = '\0';   /* strings requires a terminating null character */

/* or */

strncpy(str, "The End", 50);   /* an easier way to initialize an array with a string */

/* bad assignment */

str = "The End";   /* this is legal, however now you have a memory leak because */
                   /* free() was not called to complement the malloc() used previously. */
As I have suggested previously, re-read your book/tutorial on these basic concepts. There's no need for you to continue with your programming assignment until you have fully understood the concepts discussed above.
 
Old 04-24-2012, 07:14 AM   #28
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
hi

i implement your advice:

Code:
#define DATE_LENGTH 11 /* the length of a date's string including NULL in the end */
#define TYPE1_COST 70  /* cost for a male dog of type 1 */
#define TYPE2_COST 1000  /* monthly cost for a male dog of type 2 */
#define TYPE3_COST 100  /* cost per day for a male dog of type 3 */
#define FEMALE_EXTRA_COST 5 /* % of extra cost for female dogs */
#define DISCOUNT_PER_DOG 10 /* % of discount per dog for each dog when a person has multiple dogs */

/* types' definitions */

/* define your structs and their types here.  */





typedef struct _PENSION {

    int top1;
    int dog_id1[MAX_DOGS];
	char* dog_name1[MAX_DOGS];
    char *dog_gender1[MAX_DOGS];
    char* first_name1[MAX_DOGS];
    char* family_name1[MAX_DOGS];
    char* cell_phone1[MAX_DOGS];
 }PENSION;
typedef enum _BOOL {FALSE=0 , TRUE} BOOL;

PENSION* CreatePension();
BOOL AddDog(PENSION* ppension, int dog_id, char* dog_name, char dog_gender, char* first_name, char* family_name, char* cell_phone, char* date, int days_num);
BOOL RemoveDog(PENSION* ppension, int dog_id);
BOOL PrintDetails(PENSION* ppension, char* cell_phone);
void PrintBills(PENSION* ppension);
void FreePension(PENSION* ppension);

#endif /*_PENSION_H_*/

and i wrote pension.c:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pension1.h"

PENSION* CreatePension() {

    PENSION* arrays_pointer;
    arrays_pointer=(PENSION*) malloc(sizeof(PENSION*));


    if((arrays_pointer == NULL) ){
        exit(1);
    }

    arrays_pointer->top1=0;



    


    return arrays_pointer;

}

BOOL AddDog(PENSION* ppension, int dog_id, char* dog_name, char dog_gender, char* first_name, char* family_name, char* cell_phone, char* date, int days_num) {

    int i,a;

	char* b,c;
	char d;
/* type 1 */
    if((date == NULL) && (days_num==-1)) {

        for(i=0;i<ppension->top1;i++)
        {
			
            if((ppension->dog_id1[i]==dog_id) && ((!strcmp(cell_phone,&d)))) {
                return FALSE;
            }
        }
	
        ppension->dog_id1[ppension->top1]=dog_id;
		ppension->dog_name1[ppension->top1]=dog_name;
		ppension->first_name1[ppension->top1]=first_name;
		ppension->family_name1[ppension->top1]=family_name;
		ppension->cell_phone1[ppension->top1]=cell_phone;

        ppension->top1++;

        return TRUE;
    }
and now its copy the two first lines correctly:

Code:
ppension->dog_id1[ppension->top1]=dog_id;
		ppension->dog_name1[ppension->top1]=dog_name;
however when its get to the third line:

Code:
		ppension->first_name1[ppension->top1]=first_name;
i get an exception error and the program exit

ps the exception error occured with :

Code:
ppension->first_name1[ppension->top1]=first_name;
		ppension->family_name1[ppension->top1]=family_name;
		ppension->cell_phone1[ppension->top1]=cell_phone;
except:


Code:
ppension->dog_id1[ppension->top1]=dog_id;
		ppension->dog_name1[ppension->top1]=dog_name;
i think that now its must work (i really dont know why the first and the second line is execute correctly however the other line i got an exception

ps the main.c program didnt change

any idea?

thanks
 
Old 04-24-2012, 08:44 AM   #29
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by ofer4 View Post
any idea?
My gut instinct would be to define your structure like this:
Code:
#ifndef PENSION_H
#define PENSION_H

typedef enum { MALE, FEMALE } Gender;

typedef struct
{
    int    dog_id;
    char   dog_name[50];           /* a dog name can be no more than 50 chars long */
    Gender dog_gender;
    char   owner_first_name[50];   /* a first name can be no more than 50 chars long */
    char   owner_family_name[50];  /* same here */
    char   owner_cell_phone[15];   /* a cell phone number can be no more than 15 chars */
} Pension;

#endif
Then, in a separate header file/source file (perhaps PensionManager.h), define something that will manage the array of Pension structure objects that you require. For example:
Code:
#ifndef PENSION_MANAGER_H
#define PENSION_MANAGER_H

#include "Pension.h"

#define MAX_DOGS 500

Pension* thePensions;   /* used to reference the array of Pension objects */
int      numPensions;

void initPensions();
void addPension(/* insert function param attributes here */);
void removePension(/* insert function param attributes here */);
void etc(/* insert function param attributes here */);

#endif
PensionManager.c:
Code:
#include "PensionManager.h"
#include <stdlib.h>
#include <string.h>

void initPensions()
{
    thePensions = malloc(MAX_DOGS * sizeof(Pension));
    numPensions = 0;
}

void addPension(..., const char* dogName, ...)
{
    strncpy(thePensions[numPensions].dog_name, dogName, sizeof(thePensions[numPensions].dog_name) - 1);
    ...

    ++numPensions;   /* increment the number of Pensions that we are now managing */
}

...
Is this clearer now?

Last edited by dwhitney67; 04-24-2012 at 08:45 AM.
 
Old 04-24-2012, 10:09 AM   #30
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dwhitney67 View Post
My gut instinct would be to define your structure like this:
Code:
#ifndef PENSION_H
#define PENSION_H

typedef enum { MALE, FEMALE } Gender;

typedef struct
{
    int    dog_id;
    char   dog_name[50];           /* a dog name can be no more than 50 chars long */
    Gender dog_gender;
    char   owner_first_name[50];   /* a first name can be no more than 50 chars long */
    char   owner_family_name[50];  /* same here */
    char   owner_cell_phone[15];   /* a cell phone number can be no more than 15 chars */
} Pension;

#endif
Then, in a separate header file/source file (perhaps PensionManager.h), define something that will manage the array of Pension structure objects that you require. For example:
Code:
#ifndef PENSION_MANAGER_H
#define PENSION_MANAGER_H

#include "Pension.h"

#define MAX_DOGS 500

Pension* thePensions;   /* used to reference the array of Pension objects */
int      numPensions;

void initPensions();
void addPension(/* insert function param attributes here */);
void removePension(/* insert function param attributes here */);
void etc(/* insert function param attributes here */);

#endif
PensionManager.c:
Code:
#include "PensionManager.h"
#include <stdlib.h>
#include <string.h>

void initPensions()
{
    thePensions = malloc(MAX_DOGS * sizeof(Pension));
    numPensions = 0;
}

void addPension(..., const char* dogName, ...)
{
    strncpy(thePensions[numPensions].dog_name, dogName, sizeof(thePensions[numPensions].dog_name) - 1);
    ...

    ++numPensions;   /* increment the number of Pensions that we are now managing */
}

...
Is this clearer now?

hi

thanks alot for your help, i really appreciate it..i hope after my first program i will know how to help others too

I understand you and try to implement it...i hope i will no have problem with it

thanks in advance

Last edited by ofer4; 04-24-2012 at 10:29 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Google-Chrome Error (Error 9 (net::ERR_UNEXPECTED): Unknown error) smoooth103 Slackware 4 12-04-2010 07:42 PM
[SOLVED] php5 ./configure error: (FILENAME=- FNR=27) fatal error: internal error richinsc Linux - Software 2 07-08-2010 09:20 AM
Memory error: extended error chipkill ecc error rajivdp Linux - Hardware 1 12-07-2009 08:26 AM
Sendmail: eocket wedge , 504 error , dsn error, mail relay connection error djcs Debian 0 03-03-2009 12:41 AM
Suse CUPS error: cups(File)DoRequest error:client-error-bad-request smdonelan Linux - Hardware 6 04-17-2007 06:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration