LinuxQuestions.org
Review your favorite Linux distribution.
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 11-08-2010, 12:37 PM   #1
paliga
Member
 
Registered: Feb 2010
Posts: 39

Rep: Reputation: 0
Book example of own type variable initialization wont work


Hi...

Its an example froom a book... and it does not work...

Code:
#include <stdio.h>

int main ( int argc, const char * argv[] ) {

        struct a_struct {
                unsigned char first;
                unsigned char second;
        };
        typedef struct a_struct my_atyp;

        my_atyp my_var;
        my_var = {1, 2};

        printf("%c, %c", my_var.first, my_var.second);
}
compiler gives me:

Code:
playing_with_struct.c: In function ‘main’:
playing_with_struct.c:12: error: expected expression before ‘{’ token
Anybody?
Thanks in advance!

Last edited by paliga; 11-08-2010 at 12:38 PM.
 
Old 11-08-2010, 12:46 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by paliga View Post
Hi...

Its an example froom a book... and it does not work...

Code:
#include <stdio.h>

int main ( int argc, const char * argv[] ) {

        struct a_struct {
                unsigned char first;
                unsigned char second;
        };
        typedef struct a_struct my_atyp;

        my_atyp my_var;
        my_var = {1, 2};

        printf("%c, %c", my_var.first, my_var.second);
}
compiler gives me:

Code:
playing_with_struct.c: In function ‘main’:
playing_with_struct.c:12: error: expected expression before ‘{’ token
Anybody?
Thanks in advance!
It is not initialization, it is assignment. Look up initialization in http://www.open-std.org/JTC1/SC22/wg...docs/n1124.pdf .
 
Old 11-08-2010, 01:23 PM   #3
paliga
Member
 
Registered: Feb 2010
Posts: 39

Original Poster
Rep: Reputation: 0
well... now I know it has to be in the same line... kind of weird...

in the case of struct initialization and assignment have to be in the same line...
 
Old 11-08-2010, 01:27 PM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Sergei is correct. This is assignment:
Code:
my_atyp my_var;
my_var = {1, 2};
This is initialization:
Code:
my_atyp my_var = {1, 2};
Furthermore, once you correct that and run it, the output won't make much sense to you unless you change that line to this:
Code:
my_atyp my_var = {'1', '2'};
Making that correction fills those fields not with the numbers 1 and 2, which you originally coded, but the ASCII characters "1" and "2", which numerically are hex 31 and hex 32 (decimal 49 and 50), respectively. The %c character spits out single ASCII characters exactly as they are in your data. Hex 01 and hex 02 are, of course, also ASCII characters, but they won't ordinarily show up on your teletype (or, if you're a young person, your screen).

Hope this helps.
 
Old 11-08-2010, 01:31 PM   #5
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by paliga View Post
in the case of struct initialization and assignment have to be in the same line...
No, that's not quite Sergei's point.

You're not doing both "initialization and assignment" in the same line. In that one line, you're doing initialization. Initialization is what happens when you put data in the variable at the same time you declare it, which is what you're doing in the corrected code. Assignment is what you can do later, on some other line, any time you want:
Code:
my_var.first = '3';

/* and / or: */

my_var.second = '4';
 
  


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
little confusion about variable initialization. pinga123 Linux - Newbie 8 07-23-2010 07:23 AM
[SOLVED] c++ variable type followed by * bluegospel Programming 3 07-01-2010 09:57 AM
C++ variable type trick... ArthurHuang Programming 4 02-15-2008 07:41 PM
Guitar Hero type game wont work in linux hanez Linux - Games 5 12-28-2007 08:07 PM
c++ initialization lists (error, non aggregate type) Que? byteframe Programming 2 12-15-2005 06:58 PM

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

All times are GMT -5. The time now is 07:09 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