LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-17-2023, 12:11 AM   #16
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,226

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320

Apart from the fact that COLOR isn't declared (as pointed out by the compiler errors), what is this even supposed to do?
Code:
[COLOR="YELLOW"]*((int *)(p)+i));
The part with the square brackets isn't C.

Last edited by dugan; 06-17-2023 at 12:13 AM.
 
1 members found this post helpful.
Old 06-17-2023, 12:47 PM   #17
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by dugan View Post
Apart from the fact that COLOR isn't declared (as pointed out by the compiler errors), what is this even supposed to do?
Code:
[COLOR="YELLOW"]*((int *)(p)+i));
The part with the square brackets isn't C.
Sorry, for being too careless.
Have deleted that part, but the original question is still there.
 
Old 06-17-2023, 01:58 PM   #18
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Here is the fixed version of the original program (make sure not to modify it before testing):
Code:
#include <stdio.h>

void foo(int type, unsigned n, void *p)
{
    if (type=='C') {
        for (unsigned i=0; i<n; ++i) {
            printf("%d: '%c'\n", i, ((char *)p)[i]);
        }
    } else if (type=='I') {
        for (unsigned i=0; i<n; ++i) {
            printf("%d: %d\n", i, ((int *)p)[i]);
        }
    }
}

int main()
{
    int x[10] = {1,2,3,4,5,6,7,8,9,10};
    char c[5] = "ABCDE";

    foo('I', 10, &x);
    foo('C',  5, &c);
    return 0;
}
 
1 members found this post helpful.
Old 06-18-2023, 04:42 AM   #19
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
You'd really be much better off just sticking with individual fooI() and fooC() functions. There's a lot of "Just because you can, doesn't mean you should" with C and abusing void pointers to make C work like a dynamically typed language is going to lead to tears somewhere down the line.
 
1 members found this post helpful.
Old 06-18-2023, 07:52 PM   #20
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,226

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by GazL View Post
There's a lot of "Just because you can, doesn't mean you should" with C and abusing void pointers to make C work like a dynamically typed language is going to lead to tears somewhere down the line.
There's a famous rant about a library that took this too far:

https://what.thedailywtf.com/topic/15001/enlightened
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pthread giving error: invalid conversion from ‘void* (*)(int*)’ to ‘void* (*)(void*)’ knobby67 Programming 4 05-05-2017 10:54 AM
[SOLVED] Scheme -> Lisp. How can I pass a function as parameter to another function ? muggabug Programming 2 12-26-2012 10:23 AM
[SOLVED] How can i pass an entire Array into a function parameter by value? (no pointer pass) esgol Programming 26 07-31-2012 11:34 AM
( C ) How do you declare a function pointer where a parameter is a function pointer? spursrule Programming 5 11-27-2007 07:56 PM
void foo(void) and void foo() lackluster Programming 9 02-15-2003 10:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration