Click here to Skip to main content
14,870,735 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hello

I would like to ask a question regarding structures and their use with pointers.I've been trying to fix my add function, but I guess i do not understand what exactly i need to do. Thanks!
arrays.c:21:33: warning: cast to pointer from integer of different size
arrays.c:22:36: error: subscripted value is neither array nor pointer

C++
#define SIZE 5

/* initialization of both arrays and declaration of structure */

char *strings[SIZE]= {"xxx", "yyy", "zzz", "aaaa", "bbb"};

int numbers[SIZE]= {1, 2, 3, 4, 5,};

struct data {char *string;int number;};

struct data records[SIZE];

/* and here i would like to add elements from both arrays into my structure */

static void add (struct data records[], char *string, int number, int i) 
{
  

  /* this is where i want to insert elements into structure, however i get compile error*/

   records[i].string = string;
   records[i].number = number;

   i++;
}

int main(int argc, char **argv) {                                            	

 struct data records[SIZE];

  int ;
/*call add function invoking structure and both arrays */
  for (i = 0; i < SIZE; i++) 
  {
    add (records, strings[i], numbers[i], i);
    printf("name %s and age %d\n", records[i].string, records[i].number);
  }
  
  return 0;
}
Posted
Updated 15-Jul-12 1:58am
v10
Comments
Vitaly Tomilov 17-Jul-12 14:23pm
   
You have made changes to your original code that invalidated the answers below. This is not a good thing to do ;)

The lines
C++
records[count].string = (char *)strings[count];
records[count].number = numbers[count];

should be replaced with
C++
records[count].string = strings;
records[count].number = numbers;

Now your program should work as expected.

As your are already accessing the arrays in your call to the add function there is no need to do so in the function itself.

Also do not use a static variable to access the records array, instead pass the i variable from the loop to the add function and use it. Because if you were to run your loop for a second time the count variable would be out of bounds for the records array.
   
Comments
kyle007 14-Jul-12 18:33pm
   
Hello Andre, I have just modified my code just as you suggested. It compiles, but it fails to run. I cant seem to understand why.
It is hard to say, without knowing what it is your code expected to do, so we can only point out at obvious mistakes that caused those compilation statements...
C
records[count].string = (char *)strings[count];

string[count] returns type char, and not char*, and that's why that warning statement came up.
C
records[count].number = numbers[count];

This is where the error occurs, because the compiler sees number being the function parameter name, which overrides your global array with the same name, and you are trying to use simple integer as an array, is what that error means.
   
Comments
kyle007 14-Jul-12 18:20pm
   
Well, I understand that there is a casting problem but I have tried to put

records[count].string = (char)strings[count];
records[count].string = strings[count];

and it would still give casting errors. What else should I do?

Now,I know only one way to access the particular parameter of structure, and that's what I've done above when I was trying to access insert number into structure. I dont quite understand your notations, can you please suggest the actual code?

My goal, is to access the structure and insert elements from the arrays I initialized. Could it be that I declared my structure incorrectly in the first place?
Thanks a lot!
Now (looking at edit version 7) things seem to be working fine. Just two little remarks:

1. You can remove now the first definition of the records array, the one outside of main.

2. I suggest to rename the formal parameters to your add function from strings and numbers to string and number. The reason is that you are passing a single string and a single number per call, not an array.

If the strings and numbers are going to denote the name and age of a person, I'd suggest you call the structure members name and age, instead of string and number. That makes your program more readable.
   
Comments
kyle007 15-Jul-12 6:44am
   
I should've changed it before, but unfortunately it still fails to run
nv3 15-Jul-12 8:37am
   
What exactly goes wrong? What error message do you get?

Besides: You are probably not running the program as it is shown in your post. There are still syntax errors in there, for example "int ;".
C++
#include <stdio.h>
//
// code omitted
//
int i;
/*call add function invoking structure and both arrays */

for (i = 0; i < 5; i++) 
  {
    add (records, strings[i], numbers[i], i);
    printf("name %s and age %d\n", records[i].string, records[i].number);
  }
//
// code omitted
//


You don't have SIZE strings, you only have 5: "xxx", "yyy", "zzz", "aaaa", "bbb".

After the "for()" loop, the value of i is SIZE in you case. Then you try to use i as an index in the records vector. Which is an out of bounds error. The records vector has SIZE indexes: 0, 1, 2, ..., SIZE-1; but you try to print a record that is outside the vector. record[i].string = record[SIZE].string = nasty error.
   
v2
Comments
kyle007 15-Jul-12 8:02am
   
It's funny, cause I actually had correct SIZE number in my actual code, but I printf statement wasnt in the for loop, and it was crashing. Just by adding it into for loop made program to work. I honestly, dont see why it would do that, but program works! Thanks a lot Mihai and to guys who previously contributes their suggestion to this code :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900