!SGkzNqBXEJutSYnahE:matrix.org

C Programming Language

519 Members
C Programming Ask C related questions here.42 Servers

Load older messages


SenderMessageTime
29 Jan 2023
@francm:matrix.orgMtw That fails;#include <stdio.h> #include <stdlib.h> #include <string.h> int func(int **ptr) { printf("%d\n", ptr[1][2]); } int main() { int a[2][2] = { 1, 2, 3, 4 }; int **ptr = a; func(ptr); } 05:49:33
@francm:matrix.orgMtw it crash segmentation fault ./a.out 05:49:54
@an_origamian:matrix.organ_origamianfunc: 2 is not a valid index since indices start at 0. Try `ptr[0][1]` instead.06:54:01
@francm:matrix.orgMtw Index was wrong. But segmentation fault again . #include <stdio.h> #include <stdlib.h> #include <string.h> int func(int **ptr) { printf("%d\n", ptr[0][1]); } int main() { int a[2][2] = { 1, 2, 3, 4 }; int **ptr = a; func(ptr); } 07:27:29
@n_r_k:matrix.orgNRKyou cannot just convert a `int [][]` into a `int **`09:16:49
@n_r_k:matrix.orgNRKin fact, you should've gotten a warning about it09:17:31
@n_r_k:matrix.orgNRK
#include <stdio.h>

static int
func(int (*ptr)[2])
{
    printf("%d\n", ptr[0][1]);
}

extern int
main(void)
{
    int a[2][2] = { 1, 2, 3, 4 };
    int (*ptr)[2] = a;
    func(ptr);
}
09:19:04
@francm:matrix.orgMtw
In reply to @n_r_k:matrix.org
you cannot just convert a `int [][]` into a `int **`
Right. My initial question was not on how to pass array as an argument. My question was Hi, I am trying to find a C standard document or gcc compiler guide for informations on array access using pointers . I am looking for a guide which explains how [][] and deref(*) should be used.
13:55:16
@n_r_k:matrix.orgNRK draft of the C standard can be found here 14:00:15
@n_r_k:matrix.orgNRK as for your question, a is a pointer to an array, not a pointer to pointer. 14:01:13
@n_r_k:matrix.orgNRKhttps://c-faq.com/aryptr/pass2dary.html14:04:07
30 Jan 2023
@kosshi:matrix.orgkosshi changed their profile picture.13:47:49
@kosshi:matrix.orgkosshi changed their profile picture.13:50:11
@kosshi:matrix.orgkosshi changed their profile picture.13:51:51
@m-yusuf:matrix.orgM Yusuf set a profile picture.21:28:14
1 Feb 2023
@callmemarnie:matrix.org@callmemarnie:matrix.org joined the room.03:14:30
@vini_hatake99:matrix.org@vini_hatake99:matrix.org joined the room.03:47:19
@vini_hatake99:matrix.org@vini_hatake99:matrix.org left the room.03:47:34
2 Feb 2023
@seba-ian:matrix.orgSebastian Gniazdowski joined the room.11:54:18
@seba-ian:matrix.orgSebastian Gniazdowski is there any Objective C like implementation of OOP in plain C? So that one does sendmessage(obj, MSGINIT), for example? 12:04:33
@ahto:artifact8.xyzahto
In reply to @justin-mcgrath:matrix.org
I laughed. Apparently it is likely to not work though: https://stackoverflow.com/questions/6592287/volatile-and-multithreading-is-the-following-thread-safe#6592379
Oooo nasty
14:40:06
@ahto:artifact8.xyzahtoIf the concern was merely "can I get this to exit the loop", this is where the context (threads over context switch due to a signal jump) matters a lot and what you want to deem as 'safety'. If the test was "Will the write to is_terminate be visible to the thread that reads from it" the answer is probably going to be 'no' to 'depends, is it x86?' and even in the x86 case, NUMA configuration and signally can really fuck with its reliability. However, the next line of questions could be 'is safe for you, the same as what we consider safe? Are you okay with it eventually being true even if the order was invalid?'. 14:46:49
@ahto:artifact8.xyzahto
In reply to @n_r_k:matrix.org
if you have data sharing - then you're already off to a bad start
While I'd encourage to try and remove shared memory between threads and processes, it can be inevitable in many instances and the next step is really down to using the appropriate data structure for your use case.
14:53:19
@ahto:artifact8.xyzahto
In reply to @seba-ian:matrix.org
is there any Objective C like implementation of OOP in plain C? So that one does sendmessage(obj, MSGINIT), for example?
afaik, no. I guess to ask, why you looking for an OOP solution in C?
14:55:07
@ahto:artifact8.xyzahtoJust trying to understand what you want here and what OOP features you are looking for14:55:35
@an_origamian:matrix.organ_origamianGlib provides OOP, doesn't it?15:05:51
@an_origamian:matrix.organ_origamianDynamic dispatch, specifically.15:06:19
@an_origamian:matrix.organ_origamian(Which I don't think C++ has)15:06:44
@ahto:artifact8.xyzahto an_origamian:
Not sure about Glib realistically, however you can code dynamic dispatch in C it is just not a language level construct where in C++ it is
15:29:37
@ahto:artifact8.xyzahto So I guess Sebastian Gniazdowski
Are you after an OOP solution in plain C, as in, you can do OOP in C and it is plain C and compiles with any C compiler or are you after some language transformation which can be compiled by any C compiler (maybe with a plugin or some other mechanism to enable it)
15:32:26

There are no newer messages yet.


Back to Room List