Anonymous
hmm
Anonymous
it means it gives its base address in return
Pavel
Hey ! Danya Why is "abc" considered const char * and not const char[n+1] ?
well, it can be used like that, but then you bound with the size of the array and can't use it in the more generic algorithms https://ideone.com/y5rrCg
Pavel
That was C++ here goes C (have no idea how to print strings in C) https://ideone.com/j0KJEO
Anonymous
printf("%s",array_name);
Anonymous
what are the additional functionalities that you are talking about? Can you elaborate on those algorithms?
Pavel
what are the additional functionalities that you are talking about? Can you elaborate on those algorithms?
I mean if you want to make a function that does something with a string (regardless its size) you need to pass a pointer to const char anyway.
Pavel
Or you can only make a function for a string of a specific size that accepts arrays.
Anonymous
I mean if you want to make a function that does something with a string (regardless its size) you need to pass a pointer to const char anyway.
OK, that is convincing to understand. But, I am not able to make choice over what are string literals? Are they const char* or const char[]?
Pavel
OK, that is convincing to understand. But, I am not able to make choice over what are string literals? Are they const char* or const char[]?
Regarding the documentation I've sent, it's const char[N] where N is size of the string + 1. https://en.cppreference.com/w/cpp/language/string_literal But it's C++, I don't have info about C, but guess that you can Google the specification for C.
Anonymous
In the reference you provided right under the "const_cast" example, it is written that : In C, string literals are of type char[], and can be assigned directly to a (non-const) char*
Anonymous
olli
any reference would help.
http://eel.is/c++draft/lex.string#nt:string-literal
Anonymous
Check this out.
olli
any reference would help.
http://eel.is/c++draft/conv.array
Pavel
In the reference you provided right under the "const_cast" example, it is written that : In C, string literals are of type char[], and can be assigned directly to a (non-const) char*
Yes, but that's the general case of arrays. Two specific things here: first that the array is static so you don't need to care about its life time, the second is that you have a terminating character, so you don't care about storing the size. If you have (or allocate) an usual array and get a pointer to it, you need to store the size somewhere in order to work with the array through the pointer.
Anonymous
Any one pls explain this program
Anonymous
Pietrø
It does the operation for the rest
Pavel
In the same para, It is also written that : but deprecated it, as literals are const in C++
Yes, but that's another thing, it's about non-const char pointer. It's useless to have a non-const pointer to a string literal, as you should never modify the string (so const disallow the incorrect behavior)
Pietrø
With the cicle
Pietrø
Num % i means num/i rest
Pietrø
And then it controls the rest
Anonymous
So what you are saying is that string literals are immutable and storing them is illegal in char[] or pointing them by char* is also illegal.
Anonymous
and so const before char[] or char * should be used to prevent un-defined behaviour.
Anonymous
So, finally the conclusion is that : Originally, string literals are const char arrays. Right?
Anonymous
I hope you know correct but have a look at this: https://www.cs.uic.edu/~jbell/CourseNotes/C_Programming/CharacterStrings.html Check the 3rd point. Won't it give un-defined behaviour?
Anonymous
3rd point in the photo.
Pavel
3rd point in the photo.
Interesting, I didn't know that I'm actually not sure how char[] (without the size) is used.
olli
olli
By the rules of conv.array converting the type from const char[] to const char* is legal.
Anonymous
String literals are and have always been of type const char[N]
Yeah. It was just a doubt over const char[N] and const char *. It has now been cleared.
olli
In C and old C++ version "converting" from string literals to char* was allowed, although modifying results in UB. Hence C++ decided to only allow const char* which increases consistency and decreases the chance of running into UB
Anonymous
What do you say of this?
Anonymous
I hope you know correct but have a look at this: https://www.cs.uic.edu/~jbell/CourseNotes/C_Programming/CharacterStrings.html Check the 3rd point. Won't it give un-defined behaviour?
Pavel
I didn't get why the contents of s7 should not be changed, why not?
Because it's UB https://wiki.sei.cmu.edu/confluence/plugins/servlet/mobile?contentId=87152214#content/view/87152214 Not sure how it works with char[]
Pavel
Ok, seems like assigning to char[] creates a stack allocated array, that explains everything. https://www.geeksforgeeks.org/whats-difference-between-char-s-and-char-s-in-c/
Anonymous
Anonymous
Thanks. It works fine with char [] char str[] = "string literal"; str[0] = 'S';
It is an UB because it stores wtih an executable and has static storage duration. It is an UB when you are trying to change memory where the code is written
Anonymous
It is an UB because it stores wtih an executable and has static storage duration. It is an UB when you are trying to change memory where the code is written
That goes the same with a normal array containing a string literal. We are modifying the content of memory in run-time. I didn't understand what do you mean by "where the code is written?" Not trying to change memory but contents of it.
Anonymous
No, when we have an array, it copies string literal to stack and that's why you have full access
Anonymous
You should understand that when you start running your executable, all the machine code it contains goes to the RAM. And string literals are stored aside with machine code in some protected memory segment, I guess. So, changing a content of that protected memory is UB
Anonymous
Great, now string literals are stored in ROM, makes sense for causing UB. Does it only happens when use of pointer to string literal?
Yes, because it points to an actual string literal When you initilize an array with string literal, it coipies the literal to stack memory
Rani
## Rules #pinned * You are not entitled to an answer, getting angry about not answered questions will get you warned. * Not checking your problem in google (or any other search engine) first will get you a warn. * Asking for something that is in the pinned message right after joining WILL GET YOU BANNED. * C/C++ discussion preffered (assembly also allowed), asking about other languages (or groups for other languages) right after joining will get you BANNED. * Reverse enginnering, hacking and related topics are allowed, but asking to hack facebook, instagram, etc. is NOT allowed. Also if you ask "how to become a hacker" and obviously have zero knowledge on anything related to that you may get warned or banned. * Legitimate requests for help on code and programming questions are welcome. A request is considered legitimate if it describes your problem, what you've done so far and what went wrong. Requests such as "write my program" or "do my homework" are never considered legitimate. Asking not legitimate questions will result in a warn or ban. See https://stackoverflow.com/help/mcve on how to write good questions. * Asking for book recommendations is ok, but "pls give pdf book" is not allowed, find books by yourself. Posting illegally copied books (or links to those books) is also not allowed. * Only English language is allowed, if you speak shitty English or don't understand anything in English YOU WILL BE BANNED. * A little bit of programming related memes, jokes, shitposting are allowed. * NSFW content (porn, nudity, etc.) is not allowed. * Spamming/advertising (job posts are also ads, so ask an admin before posting) will grant you a warning or an immediate ban if you do it right after joining. * Religion, politics and ideological topics are forbidden. * Long code snippets must be posted via a snippet website(links below), posting pictures of code and posting long snippets in the group is not allowed. * If you encounter a problem, while using turbo C++, you will not be helped, use another more modern IDE. * Don't post compiled executables, that is obviously a security risk ## Resources About asking good questions: * [English](http://www.catb.org/esr/faqs/smart-questions.html) * [Translations](http://www.catb.org/esr/faqs/smart-questions.html#translations) For posting long code snippets: * [GitHub Gist](https://gist.github.com) * [Ubuntu Paste](https://paste.ubuntu.com/) * [Pastebin](https://pastebin.com) ## Reports If you notice any forbidden content, please use the /report command while responding to the offending post to report it to the admins.
Ok
Sudip
Any best compiler for c programme
Sudip
I am using dev c++
olli
I am using dev c++
Dev C++ is not a compiler
ॐकार
Dev C++ is not a compiler
It's IDE. right?
Anonymous
What is object oriented ?? Real life object ?
Anonymous
#include <stdio.h> void add(int a[10][10], int b[10][10], int m, int n) { int sum[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { sum[i][j] = a[i][j] + b[i][j]; printf("%d\t", sum[i][j]); } printf("\n"); } } void main() { int i,j,m,n,o,p; printf("Enter the no. of row and column of 1st matrix"); scanf("%d%d",&m,&n); printf("Enter the no. of row and column of 2nd matrix"); scanf("%d%d'",&o,&p); if(m==o && n==p) { int a[m][n]; printf("Enter the first matrix"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } int b[m][n]; printf("Enter the second matrix"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&b[i][j]); } } add(a, b, m, n); } }
Anonymous
Someone help to remove error
klimi
Anonymous
Guys does anyone have a starter book
Anonymous
Getting this error
Anonymous
Return type of main to int
Made int Now called function error
Ash
Made int Now called function error
Try to use pointers in function
Ash
So you can get the base address of the arrays
Ash
Guys anyone appearing for Kick Start 2019 - google hackathon!
Ash
?
Daniele°
So, finally the conclusion is that : Originally, string literals are const char arrays. Right?
string literals is not const, is char[] but Is ub modify Memory. On Digital Mars compiler you can write in string literals. In some mcu you can write in string literals
Daniele°
In C
Daniele°
C++ have differnt rule
Anonymous
Does it comprise from beginner to advanced
World Mobile Token Community
Hi buddies, So I'm an newbie to programming and I have encountered a case for which I suppose qualifies as an authentic question in this awesome group. I am practising building programs which are easy to maintain br localizing the effects to a class's data members by accessing and manipulating the data members through their get and set functions. In this regard I have two data members for which I wish to change. After compiling, the set functions works well by changing the values but the get functions can only return one of the data member values ata time Now is there a way to write statements inside my get functions so that I can obtain all the changed data member values without having to create multiple get functions for each data member? Regards
World Mobile Token Community
Kindly note I'm using C++