Struct Initialization

This is a discussion on Struct Initialization within the C Programming forums, part of the General Programming Boards category; I have searched but am unable to come up with a specific answer. What is the best or most efficient ...

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

    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
    22,265
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    398
    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
    398
    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
    398
    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, 10: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, 03:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21