1

My task is to count how many distinct words I have in a file using C. I already did the same assignment in Java but I have some difficulties doing this in C. I've read a lot about pointers and arrays and how to initialize them in the constructor but I still don't get it, that's why I'm asking for help.

This is how I do it in Java:

public class DataSet { private String[] elements; private int top; public DataSet() { elements = new String[1]; top = 0; } 

And now I'm trying to do the same thing in C and this is how my code looks like:

I have two classes the first one DataSet.h:

typedef struct _DataSet { char *elements; int top; } DataSet; 

and the second one DataSet.c

#include <stdlib.h> #include <stdio.h> #include "DataSet.h" /*external libaray */ DataSet* createDataSet() { DataSet* d = malloc(sizeof(TextString)); d -> elements = malloc(1 * sizeof(char)); d -> top = 0; return d; } 

I have 2 questions:

  1. are my understanding correct: I create an object DataSet in order to be able use the variables, then I set the pointer to this new array called elements which might contain one string, then I set the top to 0 and in the end i return the object.
  2. if it's not correct what should I do in order to make it exactly the same as my Java code?
7
  • 2
    You probably want an array of char* or a pointer to char* which would then be allocated an array Commented Sep 9, 2013 at 10:19
  • I'm too tired to say anything useful except that it's unnecessary and slightly bad style to multiply by sizeof(char); it's always 1 by spec. Good for not casting the return from malloc. Commented Sep 9, 2013 at 10:21
  • 1
    What is TextString? C doesn't have classes. Commented Sep 9, 2013 at 10:21
  • Removing the [java] tag as the answer won't be about Java. Commented Sep 9, 2013 at 10:26
  • I want to make string array as i did in java, but im not sure that this is the correct way to do it in C. Ive read a lot of explanations how to make String Array in C and in all of them, they use the word char instead of String (because there is no possibility to use the word String in C as a data type) Commented Sep 9, 2013 at 10:27

2 Answers 2

2

Generally if you are mallocing a struct like DataSet, you should make sure that the size of the allocation is the same as the size of the struct.

Your line

DataSet* d = malloc(sizeof(TextString)); 

Is definitely wrong, but may work by accident if TextString is bigger than DataSet. You really should write:

DataSet* d = malloc(sizeof(DataSet)); 

If you want to store an array of strings like you do in Java code, the type is wrong in this line of the DataSet struct:

char *elements; 

Remember that in C, a string is an array of characters. So an array of strings is an array of arrays of characters, like this:

char **elements; 

Also, you do not want to allocate space for one character, as you do in the line

d -> elements = malloc(1 * sizeof(char)); 

Rather you want to allocate space for a pointer to a character (the first one in the string).

d -> elements = malloc(1 * sizeof(char *)); 

Note that this does not allocate any memory for the characters themselves!

Sign up to request clarification or add additional context in comments.

3 Comments

Your elements malloc is incorrect. The pointer is in the struct, so you don't need to allocate it. You do, however, need to allocate space for the characters, and you're right that 1 probably isn't enough.
Almost, the Java class has an array of strings, I've added a bit about the struct not having the right type of elements
I've removed the downvote :) I'll add an upvote if you add a bit about writing sizeof(d->elements[0]) instead.
0

you want to allocate memory for a DataSet struct, so this is :

DataSet* d = malloc(sizeof(struct _DataSet)); 

--

d -> elements = malloc(1 * sizeof(char)); 

here you just allocate memory for one char, I think you need memory for a string :

d -> elements = malloc(n * sizeof(char)); // where n is the max length of your string 

hope this helps

2 Comments

Yes i need a memory for a string. Thank you very much for the responses.
Also note that sizeof(char) is guaranteed to be 1, by the C standard.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.