Thread: Struct Initialization

  1. #1
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404

    Struct Initialization

    I have searched but am unable to come up with a specific answer. What is the best or most efficient way to initialize a struct? Here is what the struct looks like that I am currently dealing with.
    Code:
    typedef struct id3v1
    {
    	char tag[3];
    	char title[30];
    	char artist[30];
    	char album[30];
    	char year[4];
    	char comment[29];
    	char track;
    	char genre;
    }	id3v1;
    
    id3v1 id3v1_h;
    So I have been doing this:
    Code:
    	for (i = 0; i < sizeof(id3v1_h); i++)
    		id3v1_h.tag[i] = 0;
    But is this bad because it assumes that all of the variables are stored contiguously, or is it safe to assume this because that is the nature of structs?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    But is this bad because it assumes that all of the variables are stored contiguously, or is it safe to assume this because that is the nature of structs?
    There may be padding, though how that affects your code I am not very sure. You could just write:
    Code:
    id3v1 id3v1_h = {{0}, {0}, {0}, {0}, {0}, {0}, 0, 0};
    which really would be initialisation instead of assignment.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Right on, thats what I was looking for. I took care of the padding already. Thank you.

  4. #4
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    I'm getting a problem I have gotten before but I never knew how to exactly solve it.
    Code:
    Error	1	error LNK2005: _id3v1_h already defined in check.obj	main.obj	
    Error	2	fatal error LNK1169: one or more multiply defined symbols found	C:\Users\Ben\Desktop\Music Organizer\Debug\Music Organizer.exe
    Not sure why I get this. From the message it seems like a multi-declaration. Heres the format of my files.

    main.c
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include "id3.h"
    check.c
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include "id3.h"
    id3.h
    Code:
    #ifndef ID3_H
    #define ID3_H
    
    #pragma pack(push, 1)
    typedef struct id3v1  
    {
    	char tag[3];
    	char title[30];
    	char artist[30];
    	char album[30];
    	char year[4];
    	char comment[29];
    	char track;
    	char genre;
    }	id3v1;
    #pragma pack(pop)
    
    id3v1 id3v1_h = {{0}, {0}, {0}, {0}, {0}, {0}, 0, 0};
    
    int has_id3v1(char filename[]);
    
    #endif
    Can you not initialize in header files or something?

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    Would padding be an issue if he used this command? This command would be shorter if he had a structure with many members.

    memset(&id3v1_h, 0, sizeof(struct id3v1));

  6. #6
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    I am sure I could do something similar to that, it'd just be nice if I could keep the initialization with the declaration in the header file.

    That would work because I removed the padding.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM