unclear about typecasting and creating pointers

This is a discussion on unclear about typecasting and creating pointers within the C Programming forums, part of the General Programming Boards category; I am trying make a variable (check) that will change in a while loop that pulls a single character from ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    11

    unclear about typecasting and creating pointers

    I am trying make a variable (check) that will change in a while loop that pulls a single character from an array of strings (arra). It was giving me lots of errors about creating pointers without casts, I looked up typecasting but I'm not sure if I'm understanding it very well. I ended up with this code, but I am getting the error: "warning: cast to pointer from integer of different size" on the strcpy line. Any other combination of casts gives lines of errors. Any idea what I'm still doing wrong?

    Code:
    int character = 0 ;
    int check ;
    row = 1 ;
    
    strcpy ((char *) check, (char *) arra[row][character]) ;
    printf ("first character: %s", &check) ;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Where is the while loop? You cannot use strcpy in this way, and it does not fit your description of what you are trying to do.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    'check' is an integer, so you shouldn't be pretending it's a string.
    'arra' is ... no one knows, because you didn't show us in your example. More than likely you are accessing a single character, and not a 'word'/'string'/'row' of it.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    11
    I tried creating the pointer straight out and it gave me a whole bunch of errors so I tried using strcpy. The while loops are after this part of code to go through the entire array called "arra" which i specified in my explanation. I am trying to assign "check" a single character from the array "arra". I changed "check" to an integer because the compile was giving me an error that I should be casting it as an character later. I'm very confused at this point.

    My question is this: How do I reference a single character of an array of strings and assign it to the variable "check"?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Well, it could be as simple as:
    Code:
    check = arra[0][0];
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    11
    Quote Originally Posted by laserlight View Post
    Well, it could be as simple as:
    Code:
    check = arra[0][0];
    This was my original try
    Code:
    check = arra[row][character];
    it gives this error:
    "warning: assignment makes pointer from integer without a cast"

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Post the smallest and simplest program that demonstrates the error. It should be a program of not more than say, 10 lines, that you expect to compile perfectly well but which results in that warning.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jun 2010
    Posts
    11
    I tried taking the check variable entirely out of it. This is the part of the code I am having trouble with. It is giving me a segmentation fault.

    Code:
    while (fgets (line, sizeof line, fp ) != NULL) /*read a line*/
    {
    	strcpy (arra[row], line) ;
    	row++;
    }
    				
    /*split up rows into separate arrays*/
    char codes [2000][4] ;
    char names [2000][15] ;
    char alts [2000][10];
    char lats [2000][10] ;
    char longis [2000][10] ;
    char states [2000][2];
    int character = 0 ;
    char temp ;
    			
    row = 1 ;
    
    for ( row = 1; row <2000 ; row ++) 
    	while(arra[row][character++] != ',');
    	{
    		temp = arra [row][character] ;
    		codes [row][character ] = temp ;
    	}
    	while(arra[row][character++] != ',');
    	{
    		temp = arra [row][character] ;
    		names[row][character] = temp ;
    	}
    	while(arra[row][character++] != ',');
    	{
    		temp = arra [row][character] ;
    		states[row][character] = temp ;
    	}
    	while(arra[row][character++] != ',');
    	{
    		temp = arra [row][character] ;
    		lats[row][character] = temp ;
    	}
    	while(arra[row][character++] != ',');
    	{
    		temp = arra [row][character] ;
    		longis[row][character] = temp ;
    	}
    	while(arra[row][character++] != ',');
    	{
    		temp = arra [row][character] ;
    		alts[row][character] = temp ;
    	}
    	fclose(fp) ;

  9. #9
    Registered User
    Join Date
    Jun 2010
    Posts
    11
    the fclose (fp) should be one tab back

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    The thing is, you showed even more code, yet the declaration of arra is still missing.

    EDIT:
    That said, one thing to note is that your character variable is incremented, but never reset. It looks like you do not even need temp. You are probably looking to use strtok() to tokenise the string based on ','.
    Last edited by laserlight; 06-16-2010 at 04:20 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1. 'row' should start at 0, not at 1.
    2. the 'for' loop only executes the first 'while', not all of the ones after it.
    3. You 'character' plows off the end of all of your arrays.
    4. Why do you have so many huge arrays?

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Jun 2010
    Posts
    11
    Quote Originally Posted by laserlight View Post
    The thing is, you showed even more code, yet the declaration of arra is still missing.

    EDIT:
    That said, one thing to note is that your character variable is incremented, but never reset. It looks like you do not even need temp. You are probably looking to use strtok() to tokenise the string based on ','.
    Sorry, it's kind of a long code. here's the declaration of arra:
    Code:
    char arra [2000] [100] ;
    strtok sounds like what I want to use, I'll try that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Uses for Void Pointers
    By Ashes999 in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2003, 03:29 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21