Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 962725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:33:14+00:00 2026-05-16T01:33:14+00:00

in PHP, I have something like function doStuff($in, $value) { $var = V_ .

  • 0

in PHP, I have something like

function doStuff($in, $value)  
{  
   $var = "V_" . $in;  
   $$var = $value;
}

Is there a way to do something similar in C?

Basically I’m trying to figure out how to make a sort of library to make working with IO pins on an AVR easier. So for example, there would be a function to set a particular pin to an OUTPUT. That pin in the AVR is part of PORTB. Setting it to an output and giving it a value requires me to reference DDRB and PORTB constants and set their values. Rather than going through all of that, I’d like to be able to call a function such as SetMode(Pin #, Mode);. I just can’t figure out how to do that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:33:14+00:00Added an answer on May 16, 2026 at 1:33 am

    Your question is still a little unclear (as indicated by the assortment of interpretations in the answers). I’m assuming that you want to refer to pins by physical pin number. If this is not correct, please clarify your question so we can provide better answers.

    Here’s roughly how I would do it if someone held a gun to my head:

    DISCLAIMER: I have not tested this nor been particularly careful about checking documentation. The code is written for avr-gcc/avr-libc on Linux, though it may work elsewhere.

    // Map from physical pin number to associated direction register.
    volatile uint8_t *ddr_map[] = {
        NULL, // Vcc, GND, or some other non-IO pin.
        &DDRB,
        &DDRB,
        &DDRC,
        // etc...  Values will vary for different target chips.
    };
    
    // Map from physical pin number to port mask.
    uint8_t mask_map[] = {
        0x00,
        _BV(0),
        _BV(1),
        _BV(0),
        // etc...  Values will vary for different target chips.
    }
    
    typedef enum {
        IN,
        OUT
    } PinDir;
    
    void setMode(int pin, PinDir dir) {
        if(dir == OUT) {
            *ddr_map[pin] |= mask_map[pin];
        } else {
            *ddr_map[pin] &= ~mask_map[pin];
        }
    }
    

    See http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_port_pass

    And here’s why it’s not a good idea:

    1. It doesn’t abstract away any meaningful behavior (it actually removes abstraction — the physical pin number is lower level than the logical port/pin). Moreover, the physical pin number is not necessarily the same for different package formats. The pins of PORTB may not be assigned to the same physical pin numbers on a QFP package as a PDIP package. So this code is actually more confusing.

    2. It adds overhead. You have an extra function call (which costs cycles and stack) and two (or more) arrays used for lookups (which cost flash and RAM on the AVR unless you take special measures, in which case they cost extra cycles and flash or EEPROM) not to mention all the indirections (array lookups, pointer dereferencing) and the extra compare and branch. In desktop & web development you would be right to laugh at my concern over such small costs, but on AVR that waste has considerably more impact. (NOTE: You might be able to convince the compiler to optimize some of this out, but if you are using -Os it will be difficult. And now you’re worrying about even lower level details than before…)

    3. The provided means of manipulating pins is not so complicated as to be worth hiding in this way. You should get comfortable with converting between hexadecimal and binary in your head (it’s not hard). Even if you don’t want to mess with hex, the _BV() macro makes pin manipulations pretty easy (or just use (1 << x) which is more portable and will be recognized by more programmers).

    By the way, PORTB, DDRB, etc. are not constants. They are variables that are tied to specific addresses or registers. Trying to modify a constant with something like CONST_THINGY |= 0x03 would produce a compiler error.

    Variable variables

    C does not have the feature you described. It is a low level language (it is sometimes described as “high-level assembly”) that doesn’t provide many fancy features (by today’s standards). This is why it is the language of choice for AVR — you want to be close to the hardware, and you don’t want lots of extra overhead.

    What C does have is pointers. Based on your question and comments I would guess that you aren’t very familiar with them, so here’s a quick explanation:

    • The & operator returns a pointer to a variable, and is used like this: pointer = &variable;
    • * actually has a couple of uses.
      • The first is declaring a pointer variable (i.e. a variable that holds a pointer instead of an int, char, or float): int *pointer; Notice that you have to specify what type of variable it will point at.
      • The second use is what is called dereferencing a pointer. Basically, this means accessing a variable through the pointer. If pointer points at variable, *pointer = 42; will set variable equal to 42, and other_var = *pointer will set other_var to the value of variable.
    • There is also pointer arithmetic, but that’s beyond the scope of this answer.

    The point of all this is that you can effectively treat variables themselves like values, storing them and passing them around. You can’t really modify them in any meaningful way other than manipulating their value, but you don’t need to either.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP File with something like this: <script type=text/javascript> var page =
I have a file that defines constant variables, like this: define_vars.php <? define(something,value); define(something1,value);
Can I have something like this in PHP? $variable = 'string' . function() .
I have something like this: <a href=# id=lol>LOL</a> <script type=text/javascript> $('#lol').click(function(){ $.get(<?php echo $host.'/index.php'?>,
I have something like this: $.getJSON('/scripts/commons/theScriptDoTravelBackInTime.php',{ }, function(){ // etc.. etc... } }); Is
Let's say I have something like this: <?php namespace Twitter; class Twitter { function
I'm trying to make something like function config( $string ){ require 'configuration.php'; return $config[$string];
I'm trying to have something like the title of a page generated through PHP.
I have something like this in one of my views <li <?php $isCurrent ?
I'm using CodeIgniter (because it's awesome) and I have something like: <?php echo anchor(/,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.