473,398 Members | 2,212 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,398 software developers and data experts.

Incompatible types problem in C / Variable Problem

I have two quick problems with the code I am writing:

First, I define a struct of char arrays (strings) and then try accessing the same. I get an "incompatible types in assignment" error:
Expand|Select|Wrap|Line Numbers
  1. struct {
  2.      char name[50];
  3.      char value[50];
  4. } varlist[20];
  5.  
  6. int main(int argc, char *argv[]) {
  7.      varlist[0].name = "foo";
  8.      varlist[0].value = "bar";
  9. }
.

The second question I think has to do with a pointer issue, but I'm not sure.
Expand|Select|Wrap|Line Numbers
  1. char *mymethod(char str[]){
  2.      char *result = malloc(100*sizeof(char));
  3.      strcpy(result, atoi(global_variable));
  4.      // global_variable is defined in a header file
  5.      return result;
  6. }
.
When I run it, I get a "Passing arg 1 of 'atoi' makes pointer from interger without a cast" and I get the same error for arg 2 of strcpy.

Please help, and thanks in advance.
Jun 5 '08 #1
4 2434
I have two quick problems with the code I am writing:

First, I define a struct of char arrays (strings) and then try accessing the same. I get an "incompatible types in assignment" error:
Expand|Select|Wrap|Line Numbers
  1. struct {
  2.      char name[50];
  3.      char value[50];
  4. } varlist[20];
  5.  
  6. int main(int argc, char *argv[]) {
  7.      varlist[0].name = "foo";
  8.      varlist[0].value = "bar";
  9. }
.
It will not work. You're assigning the field "name" to point to a literal (namely "foo"). What you want to do is fill in the memory already pointed by name (as you have done when defining the var). You have to do strcpy. Something like...

Expand|Select|Wrap|Line Numbers
  1. strcpy( varlist[0].name, "foo");
This will copy the string "foo" inside the memory pointed by varlist[0].name.

The second question I think has to do with a pointer issue, but I'm not sure.
Expand|Select|Wrap|Line Numbers
  1. char *mymethod(char str[]){
  2.      char *result = malloc(100*sizeof(char));
  3.      strcpy(result, atoi(global_variable));
  4.      // global_variable is defined in a header file
  5.      return result;
  6. }
.
When I run it, I get a "Passing arg 1 of 'atoi' makes pointer from interger without a cast" and I get the same error for arg 2 of strcpy.

Please help, and thanks in advance.
I think you need function itoa (integer to ascii) instead of atoi (ascii to integer). It does not make sense to give an integer as a parameter to strcpy (this function is expecting a pointer to char -i.e. a string- in arg2).
Jun 5 '08 #2
Thank you... but what is the ISO standard way to convert ints to ASCII? According to man itoa it doesn't fall under standard C.
Jun 6 '08 #3
Woops, I did not know it was not standard.

Then you can try:

sprintf( yourTargetString, "%d", yourIntVar );
Jun 6 '08 #4
Laharl
849 Expert 512MB
Yeah, sprintf is about the only standard way to do int->ascii string. I've never understood why itoa isn't standard.
Jun 6 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Lorenzo Gatti | last post by:
Last weekend I tried to install Pygtk2 on Windows. I succeeded (maybe), but I discovered a likely bug. I have Windows 2000, Python 2.3.3 (with ActivePython installed after the normal version) and...
0
by: Trophy Thief | last post by:
I'm getting an odd error when I try to run a select statement in my stored procedure. I need to select records where one field = the first host variable, and the second host variable is between the...
2
by: Jean | last post by:
Hello everyone, I was having the following problem with a query, and after failing to find a similar solution on these newsgroups I decided to post here. I am quite new to Access, so would...
7
by: Brian Stubblefield | last post by:
Dear clc members, I am new to C and am posting several messages concerning a large C program that I am debugging. I am encountering a "incompatible types in assignment" warning for the...
9
by: Ali | last post by:
incompatible warning in C/C++ I am using MS Visual C/C++ 6.0. I get the following warning when compiling C:model.ex.c(1221) : warning C4133: '=' : incompatible types - from 'struct s_lsp_def...
4
by: duduch_1er | last post by:
Hello i tried this code in my stylesheet : xsl:variable name="items" select="xsl-usexsl:getElementsBySplitString(.,'i')" /> whith the java function getElementsBySplitString here the code : ...
13
by: william | last post by:
code segment: long int * size; char entry; ............. size=&entry; ************************************* Gcc reported 'assignment of incompatible pointer type'.
5
by: canteyn | last post by:
/* File Name: lab7.c Author: Nick Cantey Description: This application reads a binary file of employee information sorts by last name and prints the sorted...
6
by: fishwater00 | last post by:
Question 1: typedef struct { int x, y, z ; float *wave; float *velocity; }Model; void a(Model *domain); void b(Model *domain, int t);
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.