Thread: unclear about typecasting and creating pointers

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    Well, it could be as simple as:
    Code:
    check = arra[0][0];
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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