初始化struct变量数组 - C.

时间:2015-11-03 10:44:14

标签: c

我在C中有这个结构数组:

struct Stream_Bufer {
    int size_;
    int capacity_max;
    int p_read;
    int p_write;
    char data_[320000]; 
} stream_buffer_[30];

但如果我发出int capacity_max = 320000;,我将收到错误:“不允许使用数据成员初始值设定项”。我发现,初始化的一种方法是:

for(int i = 0; i<30;i++){
    stream_buffer_[i].capacity_max = 320000;
}

任何“干净”的方式来做到这一点?

2 个答案:

答案 0 :(得分:-2)

在这里工作,即使是在c89模式下:

<script type="text/javascript">

    $(document).ready(function () {

        var styleArray = [{
            "featureType": "administrative.locality",
            "elementType": "labels.text.fill",
            "stylers": [{
                "color": "#365B6A"
            }]
        }, {
            "featureType": "road.highway",
            "elementType": "labels.text",
            "stylers": [{
                "visibility": "on"
            }]
        }, {
            "featureType": "landscape.man_made",
            "elementType": "geometry.fill",
            "stylers": [{
                "color": "#c4d3dc"
            }, {
                "weight": 0.1
            }]
        }, {
            "featureType": "landscape.man_made",
            "elementType": "geometry.stroke",
            "stylers": [{
                "color": "#ffffff"
            }]
        },

            {
                "featureType": "landscape.natural",
                "elementType": "all",
                "stylers": [{
                    "visibility": "simplified"
                }, {
                    "color": "#e1e9ee"
                }]
            }, {
                "featureType": "poi",
                "elementType": "all",
                "stylers": [{
                    "visibility": "off"
                }]
            }, {
                "featureType": "water",
                "elementType": "all",
                "stylers": [{
                    "visibility": "on"
                }]
            }, {
                "featureType": "transit.station",
                "elementType": "all",
                "stylers": [{
                    "visibility": "off"
                }]
            }, {
                "featureType": "road.highway",
                "elementType": "geometry.fill",
                "stylers": [{
                    "color": "#cfc824"
                }]
            }, {
                "featureType": "road.highway",
                "elementType": "geometry.stroke",
                "stylers": [{
                    "color": "#cfc824"
                }]
            }, {
                "featureType": "road.arterial",
                "elementType": "geometry.stroke",
                "stylers": [{
                    "color": "#4d6d7a"
                }, {
                    "weight": 0.5
                }]
            }, {
                "featureType": "road.arterial",
                "elementType": "labels.text.fill",
                "stylers": [{
                    "color": "#4d6d7a"
                }]
            }, {
                "featureType": "road.local",
                "elementType": "labels.text.fill",
                "stylers": [{
                    "color": "#4d6d7a"
                }]
            }, {
                "featureType": "road.highway",
                "elementType": "labels.text.fill",
                "stylers": [{
                    "color": "#385c6b"
                }]
            }];


        var submoem = '/media/image/marker.png';
        var primarymoem = '/media/image/marker-main.png';

        var geocoder;
        var markersArray = [];

        var locationsArray = [
            '{$Data.bwrk_gmap_substore1}',
            '{$Data.bwrk_gmap_substore2}',
            '{$Data.bwrk_gmap_substore3}',
            '{$Data.bwrk_gmap_substore4}'
        ];

        function plotMarkers() {
            for (var i = 0; i < locationsArray.length; i++) {

                codeAddresses(locationsArray[i]);

            }
        }


        geocoder = new google.maps.Geocoder();
        latlang = geocoder.geocode({
            'address': '{$Data.bwrk_gmap_mainstore}'
        }, function (results, status) {

            marker = new google.maps.Marker({
                map: map,
                icon: primarymoem,
                position: results[0].geometry.location
            });

            google.maps.event.trigger(map, 'resize');
            map.setCenter(results[0].geometry.location);



            markersArray.push(marker);
        });


        var mapOptions = {
            {if $Data.bwrk_gmap_style}styles: styleArray, {/if}
            center: latlang,
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.ROADMAP,

            mapTypeControl: {if $Data.bwrk_gmap_maptypecontrol}false{else}true{/if},
            scaleControl: {if $Data.bwrk_gmap_scaleControl}false{else}true{/if},
            zoomControl: {if $Data.bwrk_gmap_zoomControl}false{else}true{/if},
            panControl: {if $Data.bwrk_gmap_panControl}false{else}true{/if},
            navigationControl: {if $Data.bwrk_gmap_navigationControl}false{else}true{/if},
            streetViewControl: {if $Data.bwrk_gmap_streetViewControl}false{else}true{/if}
        };

        var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
        plotMarkers();

        function codeAddresses(address) {
            geocoder.geocode({
                'address': address
            }, function (results, status) {

                if (results.length > 0) {
                    new google.maps.Marker({
                        map: map,
                        icon: submoem,
                        position: results[0].geometry.location
                    });
                }
            });
        }
    });

    $.ajax({
        async: false,
        url: this.href,
        success: function (result) {
            console.log('success');
        },
        error: function (xhr, ajax, err) {
            console.error('error: ' + JSON.stringify(xhr));
            console.error(JSON.stringify(err));
        }
    });


</script>
<style>
     #map-canvas {
        width: 100%;
        height: 100%;
    }
</style>
<div id="map-canvas"></div>

但是,如果你想摆脱重复的常量,你可以使用:

void do_init(void)
{
int ii;

for(ii = 0; ii<30;ii++){
    stream_buffer_[ii].capacity_max = 320000;
        }

}

答案 1 :(得分:-2)

对于gnu C编译器,您可以这样使用:

   struct data {
     int a;
     int b;
   } data[2] = {{ .b = 1 }, { .b = 2 }};