Anonymous
Try to add flag -pedantic-errors
Anonymous
Because gcc
Anonymous
Termux GCC is actually clang and clang is bug compatible with gcc
J
yes, what about it? it still doesn't make the array variable length. it actually makes this an array of 5 ints
Anonymous
Basically neither GCC or clang really pay attention to the standard you pass to them
Anonymous
Anonymous
Explain why Linux is -std=gnu89 for the kernel and still has VLAs
Anonymous
Because GCC doesn't care and clang also implements all the mistakes GCC does
J
variable length arrays are dynamically allocated memory structures on the stack. in the case of main, which is called only once, you can't have a VLA, because it is never re-entered or called repeatedly.
Anonymous
That's what bug compatible means
Anonymous
Regardless of what standard you specify you will be able to use newer features
Anonymous
Anonymous
Don't use VLA anyway
J
you aware that that's not valid C?
Anonymous
Anonymous
Terrible
Anonymous
Anonymous
Just call malloc or use something like dprintf to avoid the VLA
Anonymous
Anonymous
Anonymous
Anonymous
C++ just based on C before VLAs became a thing
Anonymous
But compilers are free to implement this
Anonymous
I'm talking about only ISO C++
Anonymous
Does it work?
Anonymous
you did not assign, you initialised. those are completely different things
Anonymous
Nice
J
~/tmp/vla % cat main.c
#include <stdlib.h>
#include <stdio.h>
int f(size_t);
int
main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
{
int a;
a = scanf("%d", &a);
int array[a];
printf("oh no, our VLA decayed into a pointer: %zu @ %p\n",
sizeof(array), (void *)array);
f(100);
f(200);
f(300);
return EXIT_SUCCESS;
}
int f(size_t siz)
{
int vla[siz];
printf("vla of %zu bytes @ %p\n", sizeof(vla), (void *)vla);
return 0;
}
~/tmp/vla % cc main.c -Wall -Wextra -std=c99 -pedantic -o main
~/tmp/vla % echo 5 | ./main
oh no, our VLA decayed into a pointer: 4 @ 0x7fff68fc8210
vla of 400 bytes @ 0x7fff68fc8040
vla of 800 bytes @ 0x7fff68fc7eb0
vla of 1200 bytes @ 0x7fff68fc7d20
~/tmp/vla % echo 20 | ./main
oh no, our VLA decayed into a pointer: 4 @ 0x7ffdd4a7b730
vla of 400 bytes @ 0x7ffdd4a7b560
vla of 800 bytes @ 0x7ffdd4a7b3d0
vla of 1200 bytes @ 0x7ffdd4a7b240
~/tmp/vla %
Alex
Anonymous
Lol
Anonymous
lmao
Anonymous
good catch
Anonymous
Anonymous
@Neko_cpp why
Deepak
Can anybody please help me by suggesting any topic in building a miniproject in C using data structures?
Deepak
I can write the code, I just want an unique topic, we are not allowed to use stacks and queues, we can use linked list, trees or graphs
Dima
bruh
أسامة
Thermionic
int year;
printf("enter a year to test: \n");
scanf("%d",&year);
if (year%4==0&&year%100!=0){
printf("it is a leap year");
}
else if (year%4==0&&year%100==0&&year%400==0)
{
printf("it is a leap year");
}
else
{
puts("COMMON YEAR");
}
return 0; Is this valid test for all leap years?
Thermionic
Anonymous
Anonymous
/start@MissRose_bot
Vlad
And nobody gets why it's working :P
Thermionic
am still learning C before I try C++.the bitwise and is currently still difficult for me to understand as used in this solution.
Thermionic
oh that is a clever .thanks for the explanation.
Anonymous
oh right
Anonymous
Alex
global vars can be inited or not. depends on this .data or .bss segment is chosen
Anonymous
https://en.wikipedia.org/wiki/.bss
second paragraph
Anonymous
Just blackbox man
Alex
why should OS know about vars?
Alex
yes
Alex
what do you mean segments of memory?
Alex
in your file there are segments of memory like .bss, .text and so on
OS just allocates pages for each segment
Anonymous
@Johnn_D0e try learning reverse engineering
MᏫᎻᎯᎷᎷᎬᎠ
Lol
Anonymous
if u learn binary u can also translate the 1s n 0s to code
Esan
Anyone into programming chatbot here?
Anonymous
Thanks
JoyfulTyrant
Good resources to learn stl?
Anonymous
Please nyone can help me?
c
struct TREE {
int operation;
struct TREE *left;
struct TREE *right;
char *value;
};
struct TREE *make_node(int operation, struct TREE *left, struct TREE *right, char *value) {
struct TREE *n;
n = (struct TREE *)malloc(sizeof(struct TREE));
if(n == NULL) { printf("Unable to Malloc\n"); exit(1); }
n -> operation = operation;
n -> left = left;
n -> right = right;
n -> value = value;
return n;
}
I create a parser and tree for my program. And this is how it works.
First, input (e.g.) 2 + 3 * 4 to the program. The programm will parse it according to the *BODMAS* theory and generate Tree. Each values as stored as a String. Not an Integer.
According to the Example I mentioned, the generated tree is 2 + ( 3 * 4 ).
+
/ \
/ \
* 2
/ \
/ \
3 4
Now I want to write Recursive function to print this tree. I tried this method.
c
int print_tree(struct TREE *n) {
char *left_val, right_val;
if(n -> left) {
left_val = n -> left -> value;
print_tree(n -> left);
}
if(n -> right) {
right_val = n -> right -> value;
print_tree(n -> right);
}
switch(n -> operation) {
case '+':
printf("%s %d %s\n", left_val, n -> operation, right_val);
break;
....
....
....
}
}
I want to get the output as follows. Print left and right part of one section, According to my example section means 3 + 4 But this does not work :/
please anyone can solve my problem? Can you tell me how to the function required to do this ? :tired_face:
da
int print_tree(struct TREE *n) {
if(n){
print_tree(n->left);
print_tree(n->right);
printf("%s", n->value);
}
}
da
Please nyone can help me?
c
struct TREE {
int operation;
struct TREE *left;
struct TREE *right;
char *value;
};
struct TREE *make_node(int operation, struct TREE *left, struct TREE *right, char *value) {
struct TREE *n;
n = (struct TREE *)malloc(sizeof(struct TREE));
if(n == NULL) { printf("Unable to Malloc\n"); exit(1); }
n -> operation = operation;
n -> left = left;
n -> right = right;
n -> value = value;
return n;
}
I create a parser and tree for my program. And this is how it works.
First, input (e.g.) 2 + 3 * 4 to the program. The programm will parse it according to the *BODMAS* theory and generate Tree. Each values as stored as a String. Not an Integer.
According to the Example I mentioned, the generated tree is 2 + ( 3 * 4 ).
+
/ \
/ \
* 2
/ \
/ \
3 4
Now I want to write Recursive function to print this tree. I tried this method.
c
int print_tree(struct TREE *n) {
char *left_val, right_val;
if(n -> left) {
left_val = n -> left -> value;
print_tree(n -> left);
}
if(n -> right) {
right_val = n -> right -> value;
print_tree(n -> right);
}
switch(n -> operation) {
case '+':
printf("%s %d %s\n", left_val, n -> operation, right_val);
break;
....
....
....
}
}
I want to get the output as follows. Print left and right part of one section, According to my example section means 3 + 4 But this does not work :/
please anyone can solve my problem? Can you tell me how to the function required to do this ? :tired_face:
It is wrong,forget about this🙈
J
no
Anonymous
J
well, your left/right are struct TREE's (call these nodes; the collection of nodes is a graph, which may be a tree)
J
so access them as you do your parents property, i.e. n->left.value
J
oh wait, you're printing it w/ your recursive function; my bad
J
your first condition should be "what do i do if the pointer is NULL"
da
J
as your nodes are constructed such that when there's not a child node, the pointer is NULL, i.e. a terminating value, you need to process that as well.
J
also, what does a single node capture? an expression?
Anonymous
Anonymous
J
okay, what output are you getting?
J
what should it output and what does it output?
da
int print_tree(struct TREE *n) {
if(n){
print_tree(n->left);
printf("%s", n->value);
print_tree(n->right);
}
} like this?