473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Error when assigning char* to structure member

Hi all,
The code below gives compilation error (in gcc).
"initializer element is not constant" @line 11

1 #include <stdio.h>
2 char* ptr="string";
3 //char ptr[ ]="string";
4
5 typedef struct
6 {
7 char *c;
8 int i;
9 }test_struct;
10
11 test_struct s={ptr,10};
12
13
14 int main()
15 {
16
17 return 0;
18 }

But when comment Line 2 & uncomment line 3 it works fine. It also
works fine when i move line 11 into main function.
Can anybody tell me cause of this error.

Thanks,
Arun

Nov 16 '06 #1
1 1560

Arun wrote:
Hi all,
The code below gives compilation error (in gcc).
"initializer element is not constant" @line 11

1 #include <stdio.h>
2 char* ptr="string";
3 //char ptr[ ]="string";
4
5 typedef struct
6 {
7 char *c;
8 int i;
9 }test_struct;
10
11 test_struct s={ptr,10};
12
13
14 int main()
15 {
16
17 return 0;
18 }

But when comment Line 2 & uncomment line 3 it works fine. It also
works fine when i move line 11 into main function.
Can anybody tell me cause of this error.

Thanks,
Arun
You can only initialize global variables with values that are known at
compile time. Since "char *ptr" is a variable, it fails. "char ptr[]"
causes the compiler to allocate space to hold the string, so ptr in
this case is not an lvalue (cannot be assigned to) and the compiler is
happy to initialize test_struct with it.

To highlight the difference try to imagine how "STRING" and ptr are
allocated in memory. When you declare "char *ptr", what is stored at
ptr? The ADDRESS of "string". What is stored at ptr when you declare
"char ptr[]"?

I'm not sure I did a good job of explaining this, but I hope it helped.

Nov 16 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: B. Wood | last post by:
I have a small program (see below) that demonstrates an error I am getting when I build the program on a linux platform. I first initialize an unsigned char array (lines 16). I then initialize...
13
by: - Kees van der Bent - | last post by:
/* With the following: */ typedef struct { unsigned char a : 1; unsigned char b : 1; } sss_t; sss_t sss; unsigned char ppp; main()
4
by: Rustam Asgarov | last post by:
I have a structure like this : public struct index_struct { static char IP = new char ; int port; static char FileN = new char ; int ID; int Bnum; long m_DataSize; int Version;
13
by: Kantha | last post by:
Hi all, I have declared an Union as follows typedef union { struct interrupt_bits { unsigned char c_int_hs_fs_status : 1, c_setup_intflag : 1,
2
by: venu reddy | last post by:
Hi all, I wrote an example code like this. I am getting error as " conversion to non-scalar type requested error". help me!!. #include<string.h> typedef struct { int val; char data;
3
by: Baby Lion | last post by:
hi,everyone , one Runtime error occur when running , maybe it's about assigning memory . I need your help , thanks. It's about the travelling sellman problem . //#include "stdafx.h" ...
6
by: Kinbote | last post by:
Hi, I'm trying to make a function that opens a file, reads it in line by line, puts each line into an malloc'd array, and returns the array. I suspect I'm going about it in an atypical fashion, as...
9
by: i | last post by:
#include<stdio.h> #include<conio.h> #include<process.h> #include<string.h> char ch; int n,m; void main(); char check(int,int,char); void cash(int,int,char); void debit_card(int,int,char);
4
by: ...vagrahb | last post by:
Hi, I have the following structure struct Format { char x; unsigned char a; unsigned char b; unsigned char c; char y;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.