-3

I have declared an array of size 10. but I want to use only 6/7 of it. Then how can I determine how many numbers/characters are there?

The following code there I have scanned "Stack" that means 5 characters. How to get it? Is there Any easiest way to find this? I've tried by sizeof() but it's just giving me the size that I have declared earlier.

  char str[10] ;
  scanf("%s",&str);   //scanned "Stack"
khair
  • 27
  • 6

1 Answers1

0

If you are using strings, then you can find the length of the stored value by using strlen.

However, if you want to store general integer data, C does not have mechanisms to know the size of the actual data stored.

You can create your own data structure, with a count field and an array of integers. You need to maintain this count in your program.

Depending in your application you can create functions to scan elements from user, or copy data from other array etc. As it gets more complicated you are approaching an OOP language and you may be better served by a vector in C++

Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31