Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

constructor in C

+2
−4
#include<stdio.h>

struct Book {
	char title[20];
	char author[20];
	int pages;
};

void init_Book_types(struct Book* aTitle,struct Book* aAuthor,struct Book* aPages){
	aTitle->title;
	aAuthor->author;
	aPages->pages;

	printf("Title : %s; Author : %s; Pages : %d;",title,author,pages);
}

void main(){
	struct Book title; struct Book author; struct Book pages;
	struct A *c = malloc(sizeof(struct Book));
	init_Book_types("Harry","JK",500);
	init_Book_types("Lord","Tolkein",450);
}

In Polytechnic, teachers teach student constructor in C also. I didn't know that constructor is possible in C. Yesterday I had read in a SO question

C (as many other languages) can still be used for object oriented programming.

When I first read that Polytechnic book lot of syntax were weird to me. When I looked at the code multiple times then, I understood the system of constructor.

In cpp,

#include<iostream>
using namespace std;

class Book{
	public:
		string title;
		string author;
		int pages;
		Book(string aTitle, string aAuthor, int aPages){
			title = aTitle;
			author = aAuthor;
			pages = aPages;
			
			cout << "Title : " << title << "; Author : " << author << "; Pages : " << pages << "\n";
		}
};

int main(){
	Book book1("Harry","JK",500);
	
	Book book2("Lord","Tolkein",450);
	return 0;
}

I was trying ot do the same thing in C (which code I had added at top). But, that code returns huge error.

constructor.c: In function ‘init_Book_types’:
constructor.c:14:55: error: ‘title’ undeclared (first use in this function); did you mean ‘aTitle’?
   14 |         printf("Title : %s; Author : %s; Pages : %d;",title,author,pages);
      |                                                       ^~~~~
      |                                                       aTitle
constructor.c:14:55: note: each undeclared identifier is reported only once for each function it appears in
constructor.c:14:61: error: ‘author’ undeclared (first use in this function); did you mean ‘aAuthor’?
   14 |         printf("Title : %s; Author : %s; Pages : %d;",title,author,pages);
      |                                                             ^~~~~~
      |                                                             aAuthor
constructor.c:14:68: error: ‘pages’ undeclared (first use in this function); did you mean ‘aPages’?
   14 |      printf("Title : %s; Author : %s; Pages : %d;",title,author,pages);
      |                                                                 ^~~~~
      |                                                                 aPages
constructor.c: In function ‘main’:
constructor.c:19:23: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration]
   19 |         struct A *c = malloc(sizeof(struct Book));
      |                       ^~~~~~
constructor.c:2:1: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
    1 | #include<stdio.h>
  +++ |+#include <stdlib.h>
    2 | 
constructor.c:19:23: warning: incompatible implicit declaration of built-in function ‘malloc’ [-Wbuiltin-declaration-mismatch]
   19 |         struct A *c = malloc(sizeof(struct Book));
      |                       ^~~~~~
constructor.c:19:23: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
constructor.c:20:25: warning: passing argument 1 of ‘init_Book_types’ from incompatible pointer type [-Wincompatible-pointer-types]
   20 |         init_Book_types("Harry","JK",500);
      |                         ^~~~~~~
      |                         |
      |                         char *
constructor.c:9:35: note: expected ‘struct Book *’ but argument is of type ‘char *’
    9 | void init_Book_types(struct Book* aTitle,struct Book* aAuthor,struct Book* aPages){
      |                      ~~~~~~~~~~~~~^~~~~~
constructor.c:20:33: warning: passing argument 2 of ‘init_Book_types’ from incompatible pointer type [-Wincompatible-pointer-types]
   20 |         init_Book_types("Harry","JK",500);
      |                                 ^~~~
      |                                 |
      |                                 char *
constructor.c:9:55: note: expected ‘struct Book *’ but argument is of type ‘char *’
    9 | void init_Book_types(struct Book* aTitle,struct Book* aAuthor,struct Book* aPages){
      |                                          ~~~~~~~~~~~~~^~~~~~~
constructor.c:20:38: warning: passing argument 3 of ‘init_Book_types’ makes pointer from integer without a cast [-Wint-conversion]
   20 |         init_Book_types("Harry","JK",500);
      |                                      ^~~
      |                                      |
      |                                      int
constructor.c:9:76: note: expected ‘struct Book *’ but argument is of type ‘int’
    9 | ok_types(struct Book* aTitle,struct Book* aAuthor,struct Book* aPages){
      |                                                   ~~~~~~~~~~~~~^~~~~~

constructor.c:21:25: warning: passing argument 1 of ‘init_Book_types’ from incompatible pointer type [-Wincompatible-pointer-types]
   21 |         init_Book_types("Lord","Tolkein",450);
      |                         ^~~~~~
      |                         |
      |                         char *
constructor.c:9:35: note: expected ‘struct Book *’ but argument is of type ‘char *’
    9 | void init_Book_types(struct Book* aTitle,struct Book* aAuthor,struct Book* aPages){
      |                      ~~~~~~~~~~~~~^~~~~~
constructor.c:21:32: warning: passing argument 2 of ‘init_Book_types’ from incompatible pointer type [-Wincompatible-pointer-types]
   21 |         init_Book_types("Lord","Tolkein",450);
      |                                ^~~~~~~~~
      |                                |
      |                                char *
constructor.c:9:55: note: expected ‘struct Book *’ but argument is of type ‘char *’
    9 | void init_Book_types(struct Book* aTitle,struct Book* aAuthor,struct Book* aPages){
      |                                          ~~~~~~~~~~~~~^~~~~~~
constructor.c:21:42: warning: passing argument 3 of ‘init_Book_types’ makes pointer from integer without a cast [-Wint-conversion]
   21 |         init_Book_types("Lord","Tolkein",450);
      |                                          ^~~
      |                                          |
      |                                          int
constructor.c:9:76: note: expected ‘struct Book *’ but argument is of type ‘int’
    9 | ok_types(struct Book* aTitle,struct Book* aAuthor,struct Book* aPages){
      |                                                   ~~~~~~~~~~~~~^~~~~~

I had wrote code by help of the answer.

After solving few problems my code look like this

#include<stdio.h>

struct Book {
	char title[20];
	char author[20];
	int pages;
};

void init_Book_types(struct Book* aTitle,struct Book* aAuthor,struct Book* aPages){
	aTitle->title;
	aAuthor->author;
	aPages->pages;

	printf("Title : %s; Author : %s; Pages : %d;",aTitle,aAuthor,aPages);
}

void main(){
	struct Book title; struct Book author; struct Book pages;
	init_Book_types("Harry","JK",500);
	init_Book_types("Lord","Tolkein",450);
}

Error:

constructor.c: In function ‘main’:
constructor.c:19:25: warning: passing argument 1 of ‘init_Book_types’ from incompatible pointer type [-Wincompatible-pointer-types]
   19 |         init_Book_types("Harry","JK",500);
      |                         ^~~~~~~
      |                         |
      |                         char *
constructor.c:9:35: note: expected ‘struct Book *’ but argument is of type ‘char *’
    9 | void init_Book_types(struct Book* aTitle,struct Book* aAuthor,struct Book* aPages){
      |                      ~~~~~~~~~~~~~^~~~~~
constructor.c:19:33: warning: passing argument 2 of ‘init_Book_types’ from incompatible pointer type [-Wincompatible-pointer-types]
   19 |         init_Book_types("Harry","JK",500);
      |                                 ^~~~
      |                                 |
      |                                 char *
constructor.c:9:55: note: expected ‘struct Book *’ but argument is of type ‘char *’
    9 | void init_Book_types(struct Book* aTitle,struct Book* aAuthor,struct Book* aPages){
      |                                          ~~~~~~~~~~~~~^~~~~~~
constructor.c:19:38: warning: passing argument 3 of ‘init_Book_types’ makes pointer from integer without a cast [-Wint-conversion]
   19 |         init_Book_types("Harry","JK",500);
      |                                      ^~~
      |                                      |
      |                                      int
constructor.c:9:76: note: expected ‘struct Book *’ but argument is of type ‘int’
    9 | void init_Book_types(struct Book* aTitle,struct Book* aAuthor,struct Book* aPages){
      |                                                               ~~~~~~~~~~~~~^~~~~~
constructor.c:20:25: warning: passing argument 1 of ‘init_Book_types’ from incompatible pointer type [-Wincompatible-pointer-types]
   20 |         init_Book_types("Lord","Tolkein",450);
      |                         ^~~~~~
      |                         |
      |                         char *
constructor.c:9:35: note: expected ‘struct Book *’ but argument is of type ‘char *’
    9 | void init_Book_types(struct Book* aTitle,struct Book* aAuthor,struct Book* aPages){
      |                      ~~~~~~~~~~~~~^~~~~~
constructor.c:20:32: warning: passing argument 2 of ‘init_Book_types’ from incompatible pointer type [-Wincompatible-pointer-types]
   20 |         init_Book_types("Lord","Tolkein",450);
      |                                ^~~~~~~~~
      |                                |
      |                                char *
constructor.c:9:55: note: expected ‘struct Book *’ but argument is of type ‘char *’
    9 | void init_Book_types(struct Book* aTitle,struct Book* aAuthor,struct Book* aPages){
      |                                          ~~~~~~~~~~~~~^~~~~~~
constructor.c:20:42: warning: passing argument 3 of ‘init_Book_types’ makes pointer from integer without a cast [-Wint-conversion]
   20 |         init_Book_types("Lord","Tolkein",450);
      |                                          ^~~
      |                                          |
      |                                          int
constructor.c:9:76: note: expected ‘struct Book *’ but argument is of type ‘int’
    9 | void init_Book_types(struct Book* aTitle,struct Book* aAuthor,struct Book* aPages){
      |                                                               ~~~~~~~~~~~~~^~~~~~
History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

The fact that you're trying to make a constructor isn't relevant to the fact that you're trying to pa... (1 comment)

2 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+8
−0

The warnings just say that you can't pass a string literal with type char[] to a function taking a struct Book* parameter. The function should be declared as:

void init_Book_types (const char* title, const char* author, int pages);

And in the constructor, aTitle needs to be aTitle->title when you call printf.

All string assignments have to be carried out with strcpy since C doesn't have a string class.

General advise when dealing with compiler messages is to deal with them one at a time. Fix the first on in the list, re-compile, then fix the next.

However, this is not how you do proper OO in C. You can do it, but it is somewhat cumbersome. Since you are a beginner, I'd simply advise to stay clear of OO for now. If you are curious still, there's some example here.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Self-answered Q&A about OO in C? (3 comments)
+8
−3

Since in your question you seem to be surprised about OOP being done in C, I'll add some more information.

There is a difference between Object Oriented Programming (OOP) and an OOP language, such as Java or C++.

OOP is a set of concepts and techniques, or, if you feel more enlightened, a programming paradigm (a programming "philosophy", as someone would say).

An OOP language is a language that is designed with facilities that support writing code in an OOP style.

You can write code in a OOP style in a non-OOP language, but this is usually cumbersome, and sometimes less readable if you don't follow a strict discipline.

Writing OOP code in a non-OOP language has its merits, though, because OOP encompasses concepts like encapsulation, data hiding and polymorphism that help give your code better structure (BTW, note that some of these concepts are not exclusive to OOP).

However, going "full-OOP" with a non-OOP language is not something generally advisable, unless you are an expert and you have considered pros and cons of that choice. Usually you are better off using a language that directly supports OOP.

Moreover, keep in mind that there are different kinds of OOP. For example: class-based OOP (e.g. Java, C++), prototype-based OOP (e.g. Javascript) or even more exotic variations (e.g. Lua, which uses a mechanism based on so called "metatables").

Therefore, when doing OOP in a non-OOP language you should also consider which kind of OOP you want to emulate, because the language you use could be better suited to implement one instead of another.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »