473,657 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer incompatible type assignment to character.

I have something like this:

#include <stdio.h>

main ()
{
struct line
{
char write[20];
char read[20];

struct line *next;
};

struct line n1;

n1.write= "concepts";

}

However, if i try to compile it, i get a compiler error saying that
"incompatib le types in assignment". Whats strange is that if i set
write as an integer, i don't get such a error and it compiles. Does
something special need to be done with character strings and pointers?

Thanks.
Apr 19 '06 #1
10 4106
gk245 wrote:
I have something like this:

#include <stdio.h>

main ()
{
struct line
{
char write[20];
char read[20];

struct line *next;
};

struct line n1;

n1.write= "concepts";

}

However, if i try to compile it, i get a compiler error saying that
"incompatib le types in assignment". Whats strange is that if i set
write as an integer, i don't get such a error and it compiles. Does
something special need to be done with character strings and pointers?

They are different. You have declared write as an array of 20 char and
you are attempting to assign a pointer to const char to it.

I think you are confused regarding accessing an array through a pointer
and assigning to an array. You have to copy the string literal into the
array.

--
Ian Collins.
Apr 19 '06 #2
On Wed, 19 Apr 2006 19:01:27 -0400, gk245 <to*****@mail.c om> wrote:
I have something like this:

#include <stdio.h>

main ()
{
struct line
{
char write[20];
char read[20];

struct line *next;
};

struct line n1;

n1.write= "concepts";

}


This is probably the usual confusion around arrays and pointers. You
cannot treat an array "as if" it was a pointer in the left part of an
assignment. Array names "decay" to pointers (to their first element)
when they are in the right part of an assignment, but the reverse is not
true.

You will have to use strncpy() or strlcpy() to copy the data from your
constant string into the array member of the structure, i.e. with:

size_t len;

len = sizeof(n1.write );
strncpy(n1.writ e, "concepts", len - 1);
n1.write[len - 1] = '\0';

or

strlcpy(n1.writ e, "concepts", sizeof(n1.write ));

Apr 19 '06 #3
gk245 <to*****@mail.c om> writes:
I have something like this:

#include <stdio.h>

main ()
{
struct line
{
char write[20];
char read[20];

struct line *next;
};

struct line n1;

n1.write= "concepts";

}

However, if i try to compile it, i get a compiler error saying that
"incompatib le types in assignment". Whats strange is that if i set
write as an integer, i don't get such a error and it compiles. Does
something special need to be done with character strings and pointers?


<http://www.c-faq.com/>. Read all of section 6, "Arrays and Pointers".

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 19 '06 #4
Keith Thompson formulated on Wednesday :
gk245 <to*****@mail.c om> writes:
I have something like this:

#include <stdio.h>

main ()
{
struct line
{
char write[20];
char read[20];

struct line *next;
};

struct line n1;

n1.write= "concepts";

}

However, if i try to compile it, i get a compiler error saying that
"incompatib le types in assignment". Whats strange is that if i set
write as an integer, i don't get such a error and it compiles. Does
something special need to be done with character strings and pointers?


<http://www.c-faq.com/>. Read all of section 6, "Arrays and Pointers".


Thx for the link and the help guys. ^^
Apr 20 '06 #5
> This is probably the usual confusion around arrays and pointers. You
cannot treat an array "as if" it was a pointer in the left part of an
assignment. Array names "decay" to pointers (to their first element)
when they are in the right part of an assignment, but the reverse is not
true.


Okay, I'm confused. Then how come something like this works.

include <stdio.h>
#define BUF 6

int main(void) {
char arr[BUF] = "la";

printf("the value is: %s\n", arr);

return 0;
}
Chad

$gcc -Wall arr.c -o arr
$./arr
the value is: la
But something like this:

include <stdio.h>
#define BUF 6

int main(void) {
int arr[BUF] = "la";

printf("the value is: %s\n", arr);

return 0;
}
produces
$gcc -Wall arr.c -o arr
arr.c: In function `main':
arr.c:5: error: invalid initializer
arr.c:7: warning: char format, different type arg (arg 2)

Apr 20 '06 #6
> This is probably the usual confusion around arrays and pointers. You
cannot treat an array "as if" it was a pointer in the left part of an
assignment. Array names "decay" to pointers (to their first element)
when they are in the right part of an assignment, but the reverse is not
true.


Okay, I'm confused. Then how come something like this works.
#include <stdio.h>
#define BUF 6

int main(void) {
char arr[BUF] = "la";

printf("the value is: %s\n", arr);

return 0;

}
$gcc -Wall arr.c -o arr
$./arr
the value is: la

But something like this:

#include <stdio.h>
#define BUF 6

int main(void) {
int arr[BUF] = "la";

printf("the value is: %s\n", arr);

return 0;

}

produces
$gcc -Wall arr.c -o arr
arr.c: In function `main':
arr.c:5: error: invalid initializer
arr.c:7: warning: char format, different type arg (arg 2)

Chad

Apr 20 '06 #7
Integer arrays cannot be assigned strings, you can assign one character
at a time to the array--

like arr[0]='l';
arr[1]='a';

Because when characters are assigned to integers, theri ascii value
gets transferred, but the ascii value of strings cannot be calculated.

Apr 20 '06 #8
Chad wrote:
Array names "decay" to pointers (to their first element) when they
are in the right part of an assignment, but the reverse is not true.
Okay, I'm confused. Then how come something like this works.

char arr[BUF] = "la";


This is not an assignment; it is an initialization. Some
programming languages use a different symbol for initialization
than they do for assignment. But C uses the equals sign for both.

In the initialization case, it means that "la" is an initializer for
arr. The C standard defines specifically that arrays of char can
be initialized from string literals.
int arr[BUF] = "la";


Other arrays can only be initialized by an initializer list, eg:
int arr[BUF] = { 1, 2 };

The case of initializing from a string literal is only for arrays of
char.

Apr 20 '06 #9
Groovy hepcat Ian Collins was jivin' on Thu, 20 Apr 2006 11:06:02
+1200 in comp.lang.c.
Re: Pointer incompatible type assignment to character.'s a cool scene!
Dig it!
gk245 wrote:
struct line
{
char write[20];
char read[20];
struct line *next;
};

struct line n1;

n1.write= "concepts";

[Snipage.]
They are different. You have declared write as an array of 20 char and
you are attempting to assign a pointer to const char to it.


No, he's trying to assign a pointer to char to it. There is no const
qualification on a string literal. It's not modifiable, but not const
qualified either.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Apr 22 '06 #10

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

Similar topics

2
2366
by: Morten V Pedersen | last post by:
Hi all, I'm using the following code, but there's a problem somewhere. // Typedef af 8 arrays af 7 pointere til ints #include <iostream> using namespace std; int main()
6
26935
by: Irrwahn Grausewitz | last post by:
Hey, y'all. While doing some pointer experiments I encountered a problem. I know that I know the answer already, but trying to remember I just screwed up my mind. I wonder if someone would be so kind to enlighten me, please. Consider the code below; my specific questions are embedded in the comment lines:
3
2982
by: joe bruin | last post by:
hello all. i am trying to get rid of some warnings and do "the right thing". although in this particular case, i am not sure what the right thing is. the code: typedef struct {
49
2775
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code ;-): char *cp; void *vp; void **vpp;
13
11985
by: william | last post by:
code segment: long int * size; char entry; ............. size=&entry; ************************************* Gcc reported 'assignment of incompatible pointer type'.
23
4763
by: =?iso-8859-1?q?Santiago_Urue=F1a?= | last post by:
Hi, I tried to return a pointer to a constant string, but the compiler gives the following warning if a cast is not used: warning: assignment from incompatible pointer type This is the code:
4
2222
by: lovecreatesbea... | last post by:
Gcc only gives out a warning: `assignment discards qualifiers from pointer target type' against code such as following: $ type a.c int main(void) { const char *pc; char *p = pc;
20
2222
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to think my way through what I want to do now. I don't know why - I'm fine with most other aspects of the language, but my brain goes numb when I'm reading about function pointers! I would like to have an array of structures, something like
0
8411
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8323
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7351
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.