27Onion
what is neovim
an editor in terminal
27Onion
so you can do everything without leaving the terminal now
Anonymous
middle school (in fact
🥲 u know too much
Anonymous
You should pass the array size too, like: bool f(int* ptr, size_t sz) { /* ... */ } The name of an array will decay to a pointer too much easily but you need the size in C++ and C (I assume). One of the advantages of using C++ over C is its brilliant STL containers, like std::array or std::vector in this case, which include their size with themselves.
We can do it without passing size if I do *(ptr+i) !='\0' Even I am doing memory allocation dynamically it's telling The correct length i have taken 0 and length=0 and using while(1) till it is not null character it will do i+1 and length+1
27Onion
you mean making a special data a termination flag?
Azadi
C-style array is not null terminated.
mito
C-style array is not null terminated.
If the size is not mentioned explicitly, then the null terminator is included at the end automatically.
Azadi
It is the string literal which is null terminated not an array of any type.
Anonymous
What is your code?
I was just asking him how to get size of an array using just pointer that is pointing that array and u can't access array just u can access it's ptr because it is pass as a formal argument in the function arr is not passed
Timo
Exception: if you're talking about argv, it is indeed always a null-terminated array. https://stackoverflow.com/a/11020198
Timo
True
Timo
And envp as the third possible parameter to main is also null terminated i suppose
\Device\NUL
my course uses Dev-C++, and I still use my neovim to take the course
fren 🤝, I still can't find good IDE. I prefer edit it manual from cli and then compile it
Timo
Isn't envp not defined by C/C++ standard?
Yeah I think it's not standard, but widely supported afaik
Anonymous
func(void *ptr, size_t nbyte)
But I have done without take size By pointer arithmetic making length+ until *(ptr+i) is not equal to null character
Azadi
But I have done without take size By pointer arithmetic making length+ until *(ptr+i) is not equal to null character
Like this? void foo(char* charPtr) { for(*charPtr; *charPtr != '\0'; ++charPtr) // do something with the element } int main() { char a[] {"abcd"}; foo(a); }
\Device\NUL
if (i) in C/C++ is basically if (i != 0) or if (i != '\0')
\Device\NUL
Of?
Edited
\Device\NUL
Am I wrong '-')/
Jose
Do you mean 'or'? Please use question marks if you are questioning something
Jose
if (i) in C/C++ is basically if (i != 0) or if (i != '\0')
IMHO, if(i) is a lazy way to say "you are dumb if you don't understand it". When you make an if statement, the answer can only be zero or non-zero. For example, when using an specific value, you explicitly express the value: if ( my_value == 5 ) Or in the case of chars: if ( the_element_of_that_string == 'a' ) But, the "i am smarter than everybody" kind of people uses hacks (yes, its literally a hack, a code cut), and, when you want to check the value explicitly, for example, with pointers, you could express in that way: if ( my_pointer == NULL ) But no, the usual approach is the implicit if capability (your programming teacher doesn't tell anything about). There is an implicit conversion to a "true/false" state, so, if you use the "hacky" way (remember the dictionary definition of hack: a cut) if ( my_pointer ) The if itself adds for you (even if you don't want to) the internal conversion to a "true/false". Because that means explicitly "non-zero/zero", the value of the variable you can enter there is checked specifically for zeroes -> false case, and whatever other value -> true
Jose
if (i) in C/C++ is basically if (i != 0) or if (i != '\0')
So, the answer to your question "Is the != 0 or != '\0'" is both. That's the magic! Because '\0' is de char ASCII representation of a 0x00 value in 8 bits, and the 0 is the representation of (in 32 bit machine) 0x00000000. So, strictly are not exactly the same (because of the types), but they are exactly the same (at value comparison): both compares with the non-zero/zero.
Jose
Actually I refer to this message https://t.me/programminginc/501268. > By pointer arithmetic making length+ until *(ptr+i) is not equal to null character But thanks for the additional information anyway :D
if you think *(ptr + i) could be hard to understand, in C and C++ (the last, only without overloaded operators) are exactly the same as ptr[i] or i[ptr] Pointer hacks could be useful: *ptr <-> *(ptr+0) <-> ptr[0]
Danya🔥
/blue_text_must_click
Jose
Pardon?
\Device\NUL
Didn't you know for (i = 0; i < X; i++) is same with i = 0 while (i < X) i++
Anonymous
So where's the problem?
Now there is no problem I was facing how to find the length at morning when I was solving questions in leatcode it Haven't give me size of array and was asking to sort that's why i was asking to how to get length of array using pointers . By this learned a. New way to solve it
\Device\NUL
I know this we can do this if size is given
The size is not given, and searched by finding terminators on it
\Device\NUL
The reason why strcpy doesn't need length as strncpy because it can find the end of string limit by finding the null terminator
Anonymous
The reason why strcpy doesn't need length as strncpy because it can find the end of string limit by finding the null terminator
But the array i have taken was int I will give u screenshot of code at night when I will open my laptop That's why i was confused how to do it
\Device\NUL
If you want to convert into byte just multiple it with sizeof(int)
Anonymous
Then why with int ? It just different data type that has different size
I will show u at night right now I can't access it
Anonymous
If you want to convert into byte just multiple it with sizeof(int)
See the question was this U have given an array test cases were by leatcode terminal only Not mine So the array was given by that testcase and i have to sort it but the problem was that I have only access to pointer that's pointing that's array first element. I was not knowing where to terminate thats it that's why i ask how to know length of array using pointers So my approach was that I do pointer arithmetic and find length till*(ptr+i) is not equal to null character But in int array how can null character be there i was confused here 🧐
Anonymous
int i[1] is basically char i[4]
Hm because size of int is 4bit or 2 depend on machine
\Device\NUL
Hm because size of int is 4bit or 2 depend on machine
You can even doing some string literals using integer
\Device\NUL
#include <stdio.h> int main(void) { long s[] = {8583909746840200520, 143418749551}; puts((char *)s); return 0; }
\Device\NUL
What I'm trying to explain to you that in memory it can be everything
\Device\NUL
Ohk
Have you learn about heap memory allocation?
Anonymous
By malloc calloc
\Device\NUL
\Device\NUL
Did you know what types are that malloc() and calloc() return ?
\Device\NUL
Void pointers
Great, why is it return void pointers?
\Device\NUL
Because it can be used for any data types
\Device\NUL
The memory offset is depends on the types
Anonymous
May be because we are allocation memory dynamically and we can change it to any other pointer too Because void is a generic pointer can be cast to any other pointer
Anonymous
I haven't got into much depth right now of allocating memory dynamically but I will go 😄
\Device\NUL
It doesn't matter what its types. It just what terminators you define to indicate the end of array.
GOPA
Can anyone help me with raw strings and RegEx in c++
GOPA
If possible, point me to a good resource on internet
Asdf
If possible, point me to a good resource on internet
https://www.hackerrank.com/challenges/c-tutorial-strings/problem
Asdf
If possible, point me to a good resource on internet
https://www.hackerrank.com/challenges/attribute-parser
Asdf
If possible, point me to a good resource on internet
https://www.hackerrank.com/challenges/c-tutorial-stringstream
Asdf
If possible, point me to a good resource on internet
https://www.hackerrank.com/domains/c?filters%5Bsubdomains%5D%5B%5D=c-arrays-and-strings
GOPA
@asdfqwerty1234567890 these are string manipulation
Asdf
@asdfqwerty1234567890 these are string manipulation
different things with strings (manipulation like create, copy, substitution, slice and others)
Asdf
C++ provides a nice alternative data type to manipulate strings, and the data type is conveniently called string. Some of its widely used features are the following: - Declaration: string a = "abc"; - Size: int len = a.size(); - Concatenate two strings: string a = "abc"; string b = "def"; string c = a + b; // c = "abcdef". - Accessing i^th element: string s = "abc"; char c0 = s[0]; // c0 = 'a' char c1 = s[1]; // c1 = 'b' char c2 = s[2]; // c2 = 'c' s[0] = 'z'; // s = "zbc" P.S.: We will use cin/cout to read/write a string.
GOPA
#include <regex> ... std::string regExpr("C\\+\\+"); std::string regExprRaw(R"(C\+\+)") this is how reex looks