I have a union named un.
I have a un* named up that is the address of a un named u1 (bear with me).
The pointer works fine, but I can't pass it to a function. Any alternatives?

Here is some code to illustrate (please tell me if there is some stupid mistake):

union un
{
bool a;
int b; 
char etc;
}u1;
un * up= &u1;
void randomFunctionName (un* up)
{
up->a=true
}

Recommended Answers

All 2 Replies

Your code is valid, but it does nothing. You have the following in your code:
- Declaration of a union type called 'un' (lines 1 to 6).
- Declaration of a global variable called 'u1' of type 'un' (line 6).
- Declaration of a global pointer called 'up' which is initialized to point to 'u1' (line 7).
- Declaration of a function called 'randomFunctionName' which takes a pointer to 'un' as a parameter and internally calls it 'up' (which hides the global variable 'up') (line 8).
- Definition of the function called 'randomFunctionName' (line 9 to 11).

You can notice from the list above that there is nothing other than declarations and a function definition. You have no main() function which would actually start the execution of any kind of code, and you have not called the function 'randomFunctionName' anywhere either. Don't be surprised if this does not accomplish anything.

To fill in the bits that are missing:

#include <iostream>

union un {
  bool a;
  int b;
  char etc;
};

void randomFunctionName(un* up) {
  up->a = true;
};

int main() {
  un u1; //create a local variable called 'u1'
  randomFunctionName( &u1 ); //call the function giving it the address of 'u1'
  std::cout << "u1 is now " << u1.a << std::endl;
  return 0;
};

Sorry for the ambiguity; I only put these code fragments in because I thought that my description was too brief. This is my current code;

/*The idea is to have 4 data bits, meaning 32 possiblities, a "shift key", as well as a "punctuation and number shift key". The end result is character encoding that requires two thirds of the space necessary for ascii. Copyright Marco Merlini 2011

Please note that this system was mainly designed for English and French, would probably be suitable for Italian and a few other Latin languages, but is not designed for other languages with many different letters.

The first line are the data bits with no shift, second is shift key, third is punct. and num. shift key, and the fourth is both shift keys.
Note that the accents aren't just for the example leters, they are in fact modifiers that are applicable to all letters that support them.
Also note there are placeholders (a-v) in the fourth line, described below.
a b c d e f g h i j k l m n o p q r s t u v w x y z é ê è ë ç æ
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z É Ê È Ë Ç Æ
1 2 3 4 5 6 7 8 9 0 / \ | ! @ # $ % ? & * ( ) - + = { } [ ] " '
< > « » : ; ^ ° ~ ± a b c d e f g h i j k l m n o p q r s t u v

a = Equal to or lesser than
b = Equal to or greater than
c = Trademark symbol
d = Copyright symbol
e = Registered Trademark symbol
f = Squiggly equals sign (Approximately)
g = Up arrow
h = Left arrow
i = Down arrow
j = Right arrow
l = En dash
m = Em dash
n = Bullet (the ones in microsoft word)
o = Euro symbol
p = Yen symbol
q = pound symbol
r = space
s = new line (enter)
t = tab
u = Page break
v = Pi (why not?) Nulls might be necessary, not sure about that though.
*/
#include <iostream>
#include <cstdlib>
#include <conio.h>

using namespace std;

void convToMCE (un* up, char c)
{
if (c=='a')
{
up->d4=true;
}
else
cout<<"No current handler for that character"<<endl;
}

int main()
{
    char a,b;
    //Concatenate as many of these unions as needed in a file, don't know how to do that right now, but I'm on it.
    union un
    {
        un(void)
        {
        shift=false;
        pnshift=false;
        d1=false;
        d2=false;
        d3=false;
        d4=false;
        }
        struct
        {
        bool shift;
        bool pnshift;
        bool d1;
        bool d2;
        bool d3;
        bool d4;
        };
    }u1;
    un* up=&u1;
    start:
    a=getch();
    convToMCE(up,a);
    cout<<u1.shift<<" "<<u1.pnshift<<" "<<u1.d1<<" "<<u1.d2<<" "<<u1.d3<<" "<<u1.d4<<endl;
    cout<<"Again?"<<endl;
    b=getch()
    if (b=='a')
    goto start;
    system("PAUSE");
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.