ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Please consider being more clear with your question, or providing more background about what you are looking to determine.
Your thread title and your code are different.
First off, the sizeof(char) is whatever the machine decides, but likely it is 1.
Divide that by the sizeof a zero element array and I'm unsure why you didn't get a "divide by zero" exception in your program. Unless the compiler implicitly made that a single character, where then it decided that anything divided by the same thing is always 1.
The length of a pointer is the natural length of a register on the machine you are using.
If you want the length of the pointer, then use sizeof(char *)
I have dynamically assigned an array of strings. I try to find a length of array, which means how many strings there are in the array. and i dont want to count
I have dynamically assigned an array of strings. I try to find a length of array, which means how many strings there are in the array. and i dont want to count
Taking that comment as literal.
This would therefore not be about memory size, but instead the literal count of strings you have added to this array.
Your code would have to count.
As you add strings to the array, you just bump the count of strings variable up by 1 each time.
If you add some huge chunk, or multiple huge chunks, either each chunk counts as 1, or you need to search each chunk to derive the number of strings included, such as search for newline characters and count them.
Or ...
Do you really mean that you want the entire size of this large array you have built up?
There are a number of ways to build an array of strings, you could have pre-allocated a static memory chunk, you could be building it up piece by piece as you get new strings.
Can you post a representative code clip which illustrates how you are building this array?
It does not work like that as sizeof pointer over size of pointer is of course always 1.
I suppose you saw the sizeof(something)/sizeof(something[0]) somewhere which can help you access the max number items in that array. It only works when the type size is known at compile time.
I have dynamically assigned an array of strings. I try to find a length of array, which means how many strings there are in the array. and i dont want to count
Actually I do not understand you. You either need to store the length of the array somewhere or count it. I couldn't imagine any other way.
So if you have it somewhere just use, otherwise count it.
Since you gave no information about how it was created/constructed hard to say more, but probably there are some tricks....
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.