char-pointer

why the warining deprecated conversion from string constant to 'char*' occured in the bellow program [duplicate]

牧云@^-^@ 提交于 2021-02-11 12:29:23
问题 This question already has answers here : Why is conversion from string constant to 'char*' valid in C but invalid in C++ (4 answers) Closed 1 year ago . I created a class called person with the public member function fill_data which takes two arguments as char array and int . I passed the arguments like this fill_data("tushar",30); but there shows a warning deprecated conversion from string constant to 'char*' but I don't understand why, if any help me to know | #include<iostream> #include

Getting no segmentation fault when exceeding the size that was allocated for a char * [duplicate]

被刻印的时光 ゝ 提交于 2020-01-05 04:36:15
问题 This question already has answers here : Array index out of bound in C (10 answers) Closed 4 years ago . I use malloc to allocate n (string length of y) bytes for x. However after copying y to x , I added 3 more characters including '\0' in x and i got no error. Shouldn't I get an error for trying to assign values to unallocated memory since I've allocated space enough for only 10 characters? Is this undefined behavior? #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int

Why can't I assign an array variable directly to another array variable with the '=' operator?

☆樱花仙子☆ 提交于 2019-12-28 04:27:26
问题 Why does the following assignment not work? I would like a low-level explanation if possible. Also, here's the compiler error I get: incompatible types in assignment of 'char*' to 'char [20]' class UCSDStudent { char name[20]; public: UCSDStudent( char name[] ) { //this-> name = name; does not work! Please explain why not strcopy( this -> copy, copy ); //works } }; 回答1: Because when you have a function call like this UCSDStudent( char name[] ) only the adress of the array name is copied

C: array of char pointers not working as expected dynamically

旧城冷巷雨未停 提交于 2019-12-24 12:14:15
问题 I have the below snippets from my code where I am trying to use a dynamically allocated char * array to hold strings coming from stdin. char **reference reference = calloc(CHUNK, sizeof(char *)); I am using a temporary static array to first store the string from stdin , then based on a certain condition copy it to the array of char * . I am allocating memory to individual char * in runtime. reference[no_of_ref]=malloc(strlen(temp_in) + 1); reference[no_of_ref++]=temp_in; // printf(" in temp :

Understanding the dereference, address-of, and array subscript operators in C

我的未来我决定 提交于 2019-12-11 05:38:56
问题 I have argv[] defined as a char *. Using the following printf statements: printf("%s\n",argv[1]); // prints out the entire string printf("%p\n",&argv[1]); // & -> gets the address printf("%c\n",argv[1][0]);// prints out the first char of second var printf("%c\n",*argv[1]); // It's this last one I don't understand. What does it mean to print *argv[1] ? why isn't that the same as *argv[1][0] and how come you can't print out printf("%s\n",*argv[1]); . Also, why is &*argv[1] a different address

int8_t* and char*

假装没事ソ 提交于 2019-12-10 20:43:32
问题 Why does the statement const int8_t* cstr = "asdf"; gives error invalid conversion from ‘const char*’ to ‘const int8_t*’ Aren't int8_t* and char* same? Am I missing some subtle thing here?! 回答1: const signed char* is not the same as const char* . Check your compilation settings, because that would explain it. int8_t is always (never say never =) at least everywhere i've seen) defined as signed char . 回答2: According to [18.4 Integer types]: typedef signed integer type int8_t; // optional And

Prefer Iterators Over Pointers?

一曲冷凌霜 提交于 2019-12-10 19:55:58
问题 This question is a bump of a question that had a comment here but was deleted as part of the bump. For those of you who can't see deleted posts, the comment was on my use of const char* s instead of string::const_iterator s in this answer: "Iterators may have been a better path from the get go, since it appears that is exactly how your pointers seems be treated." So my question is this, do iterators hold string::const_iterator s hold any intrinsic value over a const char* s such that

Pointer arithmetics on non-array types

℡╲_俬逩灬. 提交于 2019-12-10 13:11:51
问题 Let's consider following piece of code: struct Blob { double x, y, z; } blob; char* s = reinterpret_cast<char*>(&blob); s[2] = 'A'; Assuming that sizeof(double) is 8, does this code trigger undefined behaviour? 回答1: Quoting from N4140 (roughly C++14): 3.9 Types [basic.types] 2 For any object (other than a base-class subobject) of trivially copyable type T , whether or not the object holds a valid value of type T , the underlying bytes (1.7) making up the object can be copied into an array of

Why does my program throw a segmentation fault while using heap-allocated memory?

旧城冷巷雨未停 提交于 2019-12-08 14:52:22
问题 After writing a program to reverse a string, I am having trouble understanding why I got a seg fault while trying to reverse the string. I have listed my program below. #include <stdio.h> #include <stdlib.h> #include <string.h> void reverse(char *); int main() { char *str = calloc(1,'\0'); strcpy(str,"mystring0123456789"); reverse(str); printf("Reverse String is: %s\n",str); return 0; } void reverse(char *string) { char ch, *start, *end; int c=0; int length = strlen(string); start = string;

Generate Unique Values

北城以北 提交于 2019-12-08 02:14:51
问题 I want to create a C program to generate numbers from 0 to 999999, keeping in mind that the number generated should not have any digits that are repetitive within it. For example, "123" is an acceptable value but not "121" as the '1' is repeated. I have sourced other program codes that check if an integer has repeated digits: Check if integer has repeating digits. No string methods or arrays What is the fastest way to check for duplicate digits of a number? However these do not really solve