#include <stdio.h> int main() { char str[5] = { 0x01, 0x00, 0x00, 0x00, 0x00 }; /* a 5 byte string, all zeros */ int *i = (int *)&(str[0]); printf("*i = %d\n", *i); i = (int*)(&str[1]); printf("*i = %d\n", *i); i = (int*)(&str[2]); printf("*i = %d\n", *i); i = (int*)(&str[3]); printf("*i = %d\n", *i); }
*i = 1*i = 0*i = 0*i = -150994944
#include <stdio.h> int main() { int *x; int y = 35; x = &y; printf("%c\n", *x); return 0; }
#include <stdio.h> int main() { char *x; int y = 35; x = &y; printf("%c\n", *x); return 0; }
Edit : My theory it should print the \0 didn't work. Instead it printed O as a first character hmmm There are like 3 chars 1001011 1001111 00000000, and they are placed from top is 00000000 and the last one is 1001011 so it should read 00000000 first hmmmmm. What is my logical error here because I showed the example with 100 and 300 int numbers and how char is picking the first byte so why the first byte is not \0 hmmmm
PS.The code didn't work and the last two code "(void(*)(int))vp" are completelly not understandable for me ;D
#include <stdio.h> int main() { int *x; char y = 'd'; x = &y; printf("%d\n", *x); printf("%c\n", *x); int z, g; z = y; g = *x; printf("%d\n", z); printf("%c\n", z); printf("%d\n", g); printf("%c\n", g); return 0; }
void (*fun_ptr)(int) = fun;
(int *) malloc()
#include<stdio.h> void main () { int a = 10; int *p; int **pp; p = &a; // pointer p is pointing to the address of a pp = &p; // pointer pp is a double pointer pointing to the address of pointer p printf("address of a: %x\n",p); // Address of a will be printed printf("address of p: %x\n",pp); // Address of p will be printed printf("value stored at p: %d\n",*p); // value stoted at the address contained by p i.e. 10 will be printed printf("value stored at pp: %d\n",**pp); // value stored at the address contained by the pointer stoyred at pp }
#include <stdio.h> int main() { char* arr[3] = { "geek", "Geeks", "Geeksfor" }; for (int i = 0; i < 3; i++) { printf("Indeks : %d, Address of (arr) : %p\n",i , *(arr+i)); printf("Indeks : %d, Address of (&arr) : %p\n",i , arr+i); printf("Indeks : %d, Address of (&arr) : %p\n",i , &arr+i); } return 0; }
Indeks : 0, Address of (arr) : 0x5644e043b008 Indeks : 0, Address of (&arr) : 0x7ffd0a7a3e60 Indeks : 0, Address of (&arr) : 0x7ffd0a7a3e60 Indeks : 1, Address of (arr) : 0x5644e043b00d Indeks : 1, Address of (&arr) : 0x7ffd0a7a3e68 Indeks : 1, Address of (&arr) : 0x7ffd0a7a3e78 Indeks : 2, Address of (arr) : 0x5644e043b013 Indeks : 2, Address of (&arr) : 0x7ffd0a7a3e70 Indeks : 2, Address of (&arr) : 0x7ffd0a7a3e90
#include <stdio.h> int main() { char* arr[3] = { "geek", "Geeks", "Geeksfor" }; for (int i = 0; i < 3; i++) { printf("Indeks : %d, Address of (arr) : %p\n",i , arr+i); printf("Indeks : %d, Address of (&arr) : %p\n",i , &arr+i); } return 0; }
main.cpp: In function ‘int main()’: main.cpp:14:22: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings] 14 | char* arr[3] = { "geek", "Geeks", "Geeksfor" }; | ^~~~~~ main.cpp:14:30: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings] 14 | char* arr[3] = { "geek", "Geeks", "Geeksfor" }; | ^~~~~~~ main.cpp:14:39: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings] 14 | char* arr[3] = { "geek", "Geeks", "Geeksfor" }; | ^~~~~~~~~~ Indeks : 0, Address of (arr) : 0x7fffcac41510 Indeks : 0, Address of (&arr) : 0x7fffcac41510 Indeks : 1, Address of (arr) : 0x7fffcac41518 Indeks : 1, Address of (&arr) : 0x7fffcac41528 Indeks : 2, Address of (arr) : 0x7fffcac41520 Indeks : 2, Address of (&arr) : 0x7fffcac41540
// C Program to print Array of strings without array of pointers #include <stdio.h> int main() { char str[3][10] = { "Geek", "Geeks", "Geekfor" }; printf("String array Elements are:\n"); for (int i = 0; i < 3; i++) { printf("%s\n", str[i]); } return 0; }
printf("%s\n", arr[i]);
<pre> int* arr[5]; for(int i=0; i<5; i++) { arr[i] = (int*)malloc(sizeof(int)); *arr[i] = i; }