6 Replies - 983 Views - Last Post: 14 November 2012 - 04:33 AM Rate Topic: -----

#1 tkfla5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 13-November 12

Define a Struct...using typedef...pass by value. Help please...

Posted 13 November 2012 - 06:33 AM

I am having difficulty defining a struct that represents a cat. I need to use a typedef which I did....When I describe the cat named "MAX", It won't compile. I need to pass it in by value...


#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0


typedef struct {
	char name;
	int weight;
	int neuteredFlag;
} CAT;

void printCat(CAT);


main(){
	

	CAT max;
	max.name = MAX;
	max.weight = 12;
	max.neuteredFlag = FALSE;

	printCat(max);

	system("pause");

}

	void printCat(CAT){

		printf("   NAME       WEIGHT       NUETER    \n");
		printf(" ========    ========     ========    \n");
		printf(" %10s %2d  %i\n", max.name, max.weight, max.neuteredFlag);


	}

	


Is This A Good Question/Topic? 0
  • +

Replies To: Define a Struct...using typedef...pass by value. Help please...

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: Define a Struct...using typedef...pass by value. Help please...

Posted 13 November 2012 - 07:20 AM

First if you get compile errors post the complete error messages. Second please explain the following line:
    max.name = MAX;

Where have you defined a variable named MAX?

Second do you realize that a char can hold one and only one character? If you want to have a variable that will hold a string in C then you will need to use an array of char. You may want to view this link: Character Arrays.


Jim
Was This Post Helpful? 0
  • +
  • -

#3 tkfla5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 13-November 12

Re: Define a Struct...using typedef...pass by value. Help please...

Posted 13 November 2012 - 08:06 AM

View Postjimblumberg, on 13 November 2012 - 07:20 AM, said:

First if you get compile errors post the complete error messages. Second please explain the following line:
    max.name = MAX;

Where have you defined a variable named MAX?

Second do you realize that a char can hold one and only one character? If you want to have a variable that will hold a string in C then you will need to use an array of char. You may want to view this link: Character Arrays.


Jim


Thanks for the response.
    max.name = MAX;
...this is what I am unsure about. So, I should have a single cat called max...and it should have the name "MAX". that is how I interpret my instructions...

As for the Char...that is a stupid error due to me being new....so maybe something like char name[10]; Which is still not compiling?
Was This Post Helpful? 0
  • +
  • -

#4 tkfla5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 13-November 12

Re: Define a Struct...using typedef...pass by value. Help please...

Posted 13 November 2012 - 08:18 AM

View Posttkfla5, on 13 November 2012 - 08:06 AM, said:

View Postjimblumberg, on 13 November 2012 - 07:20 AM, said:

First if you get compile errors post the complete error messages. Second please explain the following line:
    max.name = MAX;

Where have you defined a variable named MAX?

Second do you realize that a char can hold one and only one character? If you want to have a variable that will hold a string in C then you will need to use an array of char. You may want to view this link: Character Arrays.


Jim


Thanks for the response.
    max.name = MAX;
...this is what I am unsure about. So, I should have a single cat called max...and it should have the name "MAX". that is how I interpret my instructions...

As for the Char...that is a stupid error due to me being new....so maybe something like char name[10]; Which is still not compiling?



I changed it a little...now having difficulty in the actual function...

include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0


typedef struct {
	int weight;
	char name[10];
	int neuteredFlag;
} CAT;

void printCat(CAT);


main(){
	

	CAT max = {12, "MAX", FALSE};

	printCat(max);

	system("pause");

}

	void printCat(CAT){

		printf("   NAME       WEIGHT       NUETER    \n");
		printf(" ========    ========     ========    \n");
		printf(" %10s %2d  %i\n", max.name, max.weight, max.neuteredFlag);


	}

	

The max.name in the printf is now wrong...not sure of the problem...
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: Define a Struct...using typedef...pass by value. Help please...

Posted 13 November 2012 - 08:18 AM

You can't use the assignment operator= with C-strings, please read the link I provided in my last post for explanation of how to properly use C-strings.

Also note there is a difference between "MAX" and MAX. The former is a constant character string the second denotes a variable/Macro name.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 tkfla5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 13-November 12

Re: Define a Struct...using typedef...pass by value. Help please...

Posted 13 November 2012 - 08:31 AM

View Postjimblumberg, on 13 November 2012 - 08:18 AM, said:

You can't use the assignment operator= with C-strings, please read the link I provided in my last post for explanation of how to properly use C-strings.

Also note there is a difference between "MAX" and MAX. The former is a constant character string the second denotes a variable/Macro name.

Jim



I think I have it now...Thank you for your help!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0


typedef struct {
	int weight;
	char name[10];
	int neuteredFlag;
} CAT;

void printCat(CAT);


main(){
	

	CAT max = {12, "MAX", FALSE};

	printCat(max);

	system("pause");

}

	void printCat(CAT myCat){

		printf("   NAME       WEIGHT       NUETER    \n");
		printf(" ========    ========     ========    \n");
		printf(" %10s %2d  %i\n", myCat.name, myCat.weight, myCat.neuteredFlag);


	}

	

Was This Post Helpful? 0
  • +
  • -

#7 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Define a Struct...using typedef...pass by value. Help please...

Posted 14 November 2012 - 04:33 AM

Also, while it surely is not wrong, you could rethink using all-caps names for types (that are not macros). There are conventions in programming, and one common one is that all caps name (like CAT) are most of the time reserved for constants and macros.
It can be confusing for fellow programmers seeing your code. ;)

One other convention, not as common, but still popular, is to capitalize your custom types (Cat) but never capitalize variable names. ;)
So this here is usually easily readable by most programmers:
Cat cat; // ok, Cat is a type
Cat cats[SIZE]; // SIZE is most probably a constant, whether defined as macro or const variable.
max = MAX(m,n); // MAX is most probably a macro
SOME_T v; // SOME_T is most probably a macro, it unwinds to a type, which precisely probably depends on some previous macro definitions



While this may be confusing on first glance:
int foo(CAT); // function declaration... so CAT is probably a type, but... maybe it's a macro, who knows how many parameters it unwinds to.

This post has been edited by Xupicor: 14 November 2012 - 04:34 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1