LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-11-2013, 06:51 PM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Rep: Reputation: 70
struct pointer assignment...


I'm not understanding why these assignments are failing.
Here are the struct definitions:
Code:
struct Connection {
   FILE *fp;
   struct Core *core;
};

struct Core {
	struct Config *cnf;
	struct Database *db;
};

struct Config {
	int size;
	int free_index;
	int delete_index;
};

struct Database {
   struct Information *rows;
};

struct Information {
   int index;
   char name[MAX_DATA];
   char phone[MAX_DATA];
};
Code:
void DatabaseSort(struct Connection *conn)
{
	int i;
	char *tmp1;
	char *tmp2;

	for (i = 0; i < conn->core->cnf->delete_index; i++)
		if ((strncmp(conn->core->db->rows[i].name, conn->core->db->rows[i + 1].name, MAX_DATA)) == 1) {
			tmp1 = conn->core->db->rows[i + 1].name;
			tmp2 = conn->core->db->rows[i + 1].phone;
			conn->core->db->rows[i + 1].name = conn->core->db->rows[i].name;  -----> error: incompatible types in assignment
			conn->core->db->rows[i + 1].phone = conn->core->db->rows[i].phone; -----> error: incompatible types in assignment
			conn->core->db->rows[i].name = tmp1; -----> error: incompatible types in assignment
			conn->core->db->rows[i].phone = tmp2; -----> error: incompatible types in assignment
		}
}
Can't a pointer be assigned to another pointer?

-Tristan
 
Old 02-11-2013, 06:56 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
You cannot assign to an array using the '=' operator. Use strncpy() instead. For example:
Code:
strncpy(conn->core->db->rows[i + 1].name, conn->core->db->rows[i].name, MAX_DATA - 1);
 
Old 02-11-2013, 07:09 PM   #3
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
Ah okay I see now. Thanks.

-Tristan
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C struct string pointer problem j0hnsmith Programming 7 05-09-2009 10:37 AM
extern a struct array/pointer strider82 Linux - Kernel 1 04-08-2009 11:57 PM
how to pass pointer of struct to function? jinxcat Programming 2 09-01-2005 09:29 AM
struct pointer in C LuderForChrist Programming 2 01-07-2005 07:44 AM
using struct type X as pointer in struct X. worldmagic Programming 1 10-28-2003 02:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration