LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-27-2006, 06:28 PM   #1
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Rep: Reputation: 30
c array question


why do i get an error in this code? i get an incompatible types on line

words[num_words]=string;

what am i doing wrong? what is incompatible? or how can i fix it?

Code:
#include <stdio.h>

const int max_word_length=50;

int main(){
  FILE *file;
  file=fopen("words", "r");

  char words[1000][max_word_length];

  char string[max_word_length];
  int num_words=0;

  while(fgets(string,sizeof(string),file) != NULL){
    words[num_words]=string;
    num_words++;
  }
}
 
Old 03-27-2006, 08:24 PM   #2
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48
You need to use strcpy(). Right now you're attempting to copy the address of the string not the contents. The other way would be to make it an array of char pointers... but you would then need to dynamically allocate memory (and deallocate it when finished).
 
Old 03-27-2006, 09:11 PM   #3
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Original Poster
Rep: Reputation: 30
wow... im an idiot... thats what happens when coding for multiple hours in a row
 
Old 03-27-2006, 11:49 PM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
You forget about c-array alignment (row-major) -- swap dimentions:

char words[max_word_length][1000];

Then words[k] -- is the k-th word in the list.
And (of course) you need to use strcpy.
 
Old 03-28-2006, 02:37 AM   #5
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Quote:
Originally Posted by firstfire
You forget about c-array alignment (row-major) -- swap dimentions:

char words[max_word_length][1000];
Why?
char words[1000][max_word_length];
defines an array of 1000 strings with a length of max_word_length each, and I think, that's what true_atlantis wants.

By the way, your loop-head should rather look something like this:
while(fgets(string,sizeof(string),file) != NULL && num_words < 1000)
 
  


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
Disk array question... Thaidog Linux - Hardware 3 03-06-2006 10:47 PM
quick array question ftgow Programming 5 05-24-2005 02:52 AM
a question about array pointers in C veritas Programming 12 07-18-2004 06:31 PM
c++ structure-array question deveraux83 Programming 2 01-01-2004 08:55 AM
Array Question? Gerardoj Programming 4 10-02-2003 12:21 AM

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

All times are GMT -5. The time now is 02:42 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