0

I know that we can add elements to an array by using loop. like in here

   int array[100], position, c, n, value;
   printf("Enter number of elements in array\n");
   scanf("%d", &n);
 
   printf("Enter %d elements\n", n);
 
   for (c = 0; c < n; c++)
      scanf("%d", &array[c]);

Is there any way that I can add elements to an array without specifying a position in C, Is there any method similar to the append method in python, in C language?

Aby Sebastian
  • 62
  • 1
  • 3
  • 13
  • 4
    No, in C there is no such thing. An array doesn't now how many elements it contains. An array is basically just a pointer to the first element of the array. Learn C++, there you have lots of container classes that allow you to do such things and much more – Jabberwocky Oct 19 '20 at 15:21
  • 1
    in the tags, both python and C is mentioned, whereas both differ a lot when it comes to data structures like lists which is not in C. You probably have mixed up with both these. languages – Siddhant Oct 19 '20 at 15:30
  • 1
    You just need to implement the data structure that you want and write an `append` function for it. – William Pursell Oct 19 '20 at 15:37
  • What you want is a dynamic array, this thread gives you a good look at how to implement this: https://stackoverflow.com/questions/3536153/c-dynamically-growing-array – luv4bytes Oct 19 '20 at 15:48
  • You can also check out some git repos. Here is one: https://github.com/jibsen/scv I havn't found something like `append` there, but there is `insert`-like and obviously and `get size`-like, so you can mix these two. – Przemek Oct 20 '20 at 07:02
  • @sid OP made the relation of the question to python quite clear. Many a question with more than one language tag is mistagged, but here the idea is quite clear. From the point of view of a pure C expert, without any knowledge of python, the idea of accessing an array position without index would be unanswerable weird. – Yunnosch Nov 01 '20 at 16:01
  • Aby Sebastian. I think that the comment by Jabberwocky is actually the answer you need. I.e. I do not believe that explaining how to do something similar in C, by programming all of it yourself (or using libs), would be helpful. Please clarify the question on this point. Reading the comments you can probably see that the C routiniers have trouble answering and they do not dare to actually answer "No." Please let us know whether you need more explanation (and on what) or whether the very very short answer is what you accept. – Yunnosch Nov 01 '20 at 16:04

1 Answers1

1

Since you've just edited the question, I suppose you're still looking for an answer. The ground has been pretty well covered in comments , but I'll have a go at formalizing.

It is important to understand that although they have similarities in structure and syntax, C arrays and Python lists are not equivalent. In particular,

  • C arrays have fixed number of elements, whereas Python lists have dynamic number of elements
  • C arrays have elements that are all the same type, whereas Python lists can have elements of different types
  • C arrays do not contain any kind of metadata -- their representations consist strictly of the representations of their elements

So, with that in mind,

Is there any way that I can add elements to an array without specifying a position in C, Is there any method similar to the append method in python, in C language?

No. Strictly speaking, the question does not even make sense, because C arrays have fixed length. Code such as your example draws a distinction between array elements that contain valid data and those that do not, but that is something layered on top of the array. The array itself has no inherent sense of or representation for a distinction between valid and invalid elements.

But of course, if you want something like that then you can implement it yourself, as indeed CPython does. You can even implement a structure that does not impose an inherent limit on the number of elements. The data structure itself might look something like this:

struct int_list {
    int *elements;
    size_t size;
    size_t capacity;
};

To go with that, you would want to add functions for initializing such objects, adding and removing elements, and anything else you want to do with them. C's array-indexing operator will not work with them.

Note also that if you were asking about C++ instead of C then there would be a simpler answer: use a std::vector instead of a C-style array.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157