0

I am trying to do this in C. Lets say I have a do while loop. At the start of each loop it initializes a char that contains a path (e.g /a/b/c). So after each loop the value changes (e.g /b/c/d).

What I would like to do is at the end of each loop before the start of the next loop, store the value into an Array. Since I do not know the size of my Array , I cannot use a static array in C.
I know it is easily achievable in java such as using an ArrayList to store my values, but that is java and this is C so I know it is completely different.(Sorry I just started learning C)

List<String> myList = new ArrayList<String>();  
myList.add(path);

I would like to know if there is a similar alternative in C. I have look at some example such as link but this uses int. How would store a char containing a file path.

Edit:
If possible I hope someone could provide a solution in the answers with my requirements, that is to insert char into an array or using a linked list?

Community
  • 1
  • 1
user2541163
  • 717
  • 2
  • 7
  • 22
  • Check linked list, and how to use malloc. You will probably need some hard work on learning pointer and memory management in C. – moeCake Dec 19 '13 at 06:33
  • so using a linked list is better than using a dynamic array in my situation? – user2541163 Dec 19 '13 at 06:46
  • There is no build-in dynamic array in C, you need implment that yourself. – moeCake Dec 19 '13 at 06:50
  • `C` is a big jump from `Java`. You should first learn some basics of C++, where there is a friendly alternative to `ArrayList`, that is `std::vector`. Memory management can come later. – Siyuan Ren Dec 19 '13 at 07:06
  • Ya..and it look like you need to study quite a bit about char and string in C too, this could also be quite confusing if you just move from Java. – moeCake Dec 19 '13 at 07:13
  • Hi all. It is not that I would not like to learn C++, but right now my current situation is that I will like this to be available in C. So some working/example code from you guys will really benefit me. I will look up C++ in the near future – user2541163 Dec 19 '13 at 07:15
  • 1
    How much do you know C? It seems that you don't even know how strings are handled in C because C string is `char *` not a single char. Do you know pointer arithmetic? Do you know how to `malloc` an array and `free` it? – Siyuan Ren Dec 19 '13 at 07:25
  • As I mentioned, I am new to C, so there are lots which I am not aware of. I am learning through coding so along the way I try to pick up as much as possible. Right now I just like to have a piece of code that suits my requirement ans maybe some explanation would definitely help me( not trying to be rude). Or even some links on C arrays will help – user2541163 Dec 19 '13 at 07:32
  • If you need, I can dump my solution code to you. But I really don't want to explain to you how to run when you don't even know how to walk. – Siyuan Ren Dec 19 '13 at 07:40
  • C is very dangerous if you don't even have basic knowledge. Providing simple example of solution is easy, and is also easy for you to explode your project. – moeCake Dec 19 '13 at 07:48
  • I know you guys are unhappy that you think that I put in no effort in learning a new language, but I am trying to be nice here asking for help and have been reading the basics of C such as at www.cprogramming.com and www.tutorialspoint.com. I do believe the fact that practice through coding is much better than reading and not trying them out – user2541163 Dec 19 '13 at 07:52

1 Answers1

-1

Use malloc() to dynamically allocate memory for array. you need to free the allocated memory after using by free() For the link provided by you ,I modified code as per your need. But you can't use below code as is. You need to rewrite it according to your need.

typedef struct {
  char *array;
  size_t used;
  size_t size;
} Array;

void initArray(Array *a, size_t initialSize) {
  a->array = (char *)malloc(initialSize * sizeof(char));
  a->used = 0;
  a->size = initialSize;
}

void insertArray(Array *a, char *string) {
  if (a->used == a->size) {
    a->size = strlen(string)+1;
    a->array = (char *)realloc(a->array, strlen(string)+1);
  }
  strcpy( a->array,string );

}

void freeArray(Array *a) {
  free(a->array);
  a->array = NULL;
  a->used = a->size = 0;
}
Chinna
  • 3,930
  • 4
  • 25
  • 55
  • Hi. Thanks for your answer. So storing my char would be to use insertArray(&a,path). If I want to print the values out, how would I do it? I tried fprintf(fp,"%s\n",a.array[i]) but there is a warning (Format specifies type char* but the argument has type char) – user2541163 Dec 19 '13 at 07:02
  • If a is a pointer use (fp,"%s\n",a->array), If a is variable use (fp,"%s\n",a.array), – Chinna Dec 19 '13 at 07:04
  • Why is `array` an `int *` and you assign a `char *` to it? Your code stores a `char` array instead of a string array (that is, `char**`). Your code has terrible asymptotic complexity. And the lack of const correctness. – Siyuan Ren Dec 19 '13 at 07:04
  • @C.R. I mentioned in my post as "you can't use below code as is" – Chinna Dec 19 '13 at 07:06
  • @Chinna: Your code is a complete mess. Even ignoring all of the jarring syntax errors, your algorithm is both wrong and asymptotic inefficient. The only thing that is slightly relevant to the question is your use of `realloc`. – Siyuan Ren Dec 19 '13 at 07:09
  • @C.R. Would you mind posting your version. I am in need of help here since I have been searching for hours. Really appreciate it if you do so. – user2541163 Dec 19 '13 at 07:12
  • @user2541163: The link provided by the other answer has already told you what to do. – Siyuan Ren Dec 19 '13 at 07:15
  • @C.R. Its the same code as what it is in this answer, just that Chinna edited it to suit my requirements. However you mentioned that this code is a mess, so hopefully u can post up your version for me to understand better – user2541163 Dec 19 '13 at 07:18
  • do not cast the return value for malloc! and check if malloc or realloc failed to allocate and returned NULL. When using dynamic allocation it is always better to use it this way: `char* cpTempPtr=NULL; cpTemp=realloc(...); if(cpTemp==NULL){return;} a->array=cpTemp;` this way if realloc failed for whatever reason you don't lose the current content of a->array – H_squared Dec 19 '13 at 07:19
  • @C.R. I don't have enough time to re code. That's why i just edited and posted. I am again saying DONT USE CODE AS IS. – Chinna Dec 19 '13 at 07:21