Thread: Char Pointer Issue

  1. #1
    Broken Box of Syntax
    Join Date
    May 2012
    Location
    Florida
    Posts
    19

    Question Char Pointer Issue

    **Notice**
    What I am attempting to do may not be a pointer issue. I just recently learned how to use char types and it could possibly be something I am over looking. Before this program I have never needed to use chars in C, so I am effectively learning about pointers and char at the same time.

    Outcome I Expected

    memSlot1
    and memSlot2 within the function learnStorage() were suppost to reflect the pointed char value of responseOne and responseTwo within the function responseIn().

    Actual Outcome

    In function 'learnStorage':
    Line 64: error: incompatible types in assignment
    Line 84: error: incompatible types in assignment

    Incase here is a link to my code and error message: C code by Layvian - 114 lines - codepad

    Code:
    /*
    learning.c
    05-18-2012
    Layvian
    -----------
    The purpose of this program is to store
    character values into memory using pointers.
    */
    #include <stdio.h>
    #include <stdbool.h>
    void lifeState(void);
    void learnStorage(int status, int *pStatus);
    void responseIn(char *pMemSlot1[4], char *pMemSlot2[4]);
    void responseOut(char memSlot1[4], char memSlot2[4]);
    int main(){
    lifeState();
    return 0;
    }
    /*
    Life State Function
    While this function is in the Loop the program
    will continiue to store char values in memory.
    Once ended the life state will end displaying
    the output of the two stored values and their
    address.
    */
    void lifeState(void){
    int status = 1;
    int *pStatus;
    pStatus = &status;
    do{
    learnStorage(status, pStatus);
    }while(status == 1);
    printf("End\n");
    }
    //Learn Storage Function
    void learnStorage(int status, int *pStatus){
    /*
    MEMORY SLOT 1
    Created with two types:
    1. Character
    2. Boolean
    Each has a pointer that references the originals
    address. Once the variable slot contains a char,
    it will be entered into the IF logic to switch the
    FALSE status of the "memSlot1Full", to TRUE.
    */
    char memSlot1[4];
    char *pMemSlot1[4];
    pMemSlot1 = &memSlot1;
    _Bool memSlot1Full = false;
    _Bool *pMemSlot1Full;
    pMemSlot1Full = &memSlot1Full;
    
    /*
    MEMORY SLOT 2
    Created with two types:
    1. Character
    2. Boolean
    Each has a pointer that references the originals
    address.  Once the variable slot contains a char,
    it will be entered into the IF logic to switch the
    FALSE status of the "memSlot2Full", to TRUE.
    */
    char memSlot2[4];
    char *pMemSlot2[4];
    pMemSlot2 = &memSlot2;
    _Bool memSlot2Full = false;
    _Bool *pMemSlot2Full;
    pMemSlot2Full = &memSlot2Full;
    responseIn(pMemSlot1, pMemSlot2);
    printf("%s\n", memSlot1);
    *pStatus = 0;
    }
    
    //Response Out Function
    //Response In Function
    void responseIn(char *pMemSlot1[4], char *pMemSlot2[4]){
    char responseOne[4] = "Eart";
    char responseTwo[4] = "Mars";
    *pMemSlot1 = responseOne;
    *pMemSlot2 = responseTwo;
    }
    
    //Response Out Function
    void reponseOut(char memSlot1, char memSlot2){
    }


  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    char memSlot1[4];
    char *pMemSlot1[4];
    pMemSlot1 = &memSlot1;
    You can't assign stuff to an array, and pMemSlot1 is an array of pointers to char. I'm not sure what the right thing to do here is because your code is quite confusing and, I think, unnecessarily complex. One option might be to make pMemSlot1 a single char pointer, likes so: char *pMemSlot1; and manipulate memSlot1 through that pointer. You would need to change pMemSlot2 in a similar manner, and you would have to change your responseIn function to match.

    You shouldn't ever return local arrays from a function, whether with the return keyword, or by returning their address though a pointer parameter. The memory for those arrays disappears when the function is over, and any attempt to access it later via that pointer will result in undefined behavior.

    Also, you are making space for 4 characters in your arrays, for the strings "Mars" and "Eart". That is insufficient, since you don't allow for the null terminator. You will have a buffer overflow and/or you will not have a terminated string, causing printf and other code to fail. You need a minimum of 5 spaces for "Mars" and "Eart".

  3. #3
    Broken Box of Syntax
    Join Date
    May 2012
    Location
    Florida
    Posts
    19
    I tried to originally drop the array from the program. It worked in the sense that it gives me a output, which is great but I ran into a problem with what it actually gave me.

    I originally thought it was due to the char not really holding a string and instead hold single values.


    Without the arrays here is what I got:


    �l@
    End

    This is where I wanted to be but I was hoping for actual words. I did correct the values to be [5], and [6] rather then [4] on

    Code:
    char responseOne[6] = "Earth";
    char responseTwo[5] = "Mars";
    I originally thought it was 0-->4 creating a total of 5 spaces. So in other wors I thought a char of [4] would hold 5 values.


    You shouldn't ever return local arrays from a function, whether with the return keyword, or by returning their address though a pointer parameter. The memory for those arrays disappears when the function is over, and any attempt to access it later via that pointer will result in undefined behavior.
    Now does this comment only apply to arrays, because from my understanding, return multiple values from functions was the whole point of using pointers, because with a return you can not return multiple values. I was under the assumption that is where pointers came in.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Layvian View Post
    I tried to originally drop the array from the program. It worked in the sense that it gives me a output, which is great but I ran into a problem with what it actually gave me.

    I originally thought it was due to the char not really holding a string and instead hold single values.

    Without the arrays here is what I got:

    �l@
    End
    The way to do it is to use a single pointer and move it around, so it points to the different characters in the array, and manipulate them through that array. You could use the string library functions, like strcpy et al., but I think that wouldn't allow you to learn how pointers work very well. Perhaps an easier task for starters is to implement the function strlen using just a pointer.

    This is where I wanted to be but I was hoping for actual words. I did correct the values to be [5], and [6] rather then [4] on

    Code:
    char responseOne[6] = "Earth";
    char responseTwo[5] = "Mars";
    I originally thought it was 0-->4 creating a total of 5 spaces. So in other wors I thought a char of [4] would hold 5 values.
    Ahh, yes, a common enough mistake. When you declare an array of size N, you allocate space for N objects, with indexes 0 to N-1.

    Now does this comment only apply to arrays, because from my understanding, return multiple values from functions was the whole point of using pointers, because with a return you can not return multiple values. I was under the assumption that is where pointers came in.
    It applies to more than arrays, but that's not really the issue. The issue is that arrays are not first class objects, so you can't return them using the return statement, like you can return an integer, pointer or struct. Pointers do allow you to return more information through the parameter list, but you still can't "return an array" that way. What you are trying to do in your code is basically return an array through a pointer. This is not possible. As I explained in my previous post, those arrays don't exist when the function is over. The memory they occupied becomes invalid, and may be reused for other data in this function or other functions you call. Thus, the pointer that contains their address does not point to the strings "Earth" or "Mars". To "return" an array from a function, you must declare it in the scope that calls the function, then pass in the array to be filled by the function. This is basically how strcpy works, you give it an array to copy into.

  5. #5
    Broken Box of Syntax
    Join Date
    May 2012
    Location
    Florida
    Posts
    19
    So I decided to give up on chars and strings until I understand how they work in C a little bit better but the program itself is working. I simply changed the chars to integers.

    Even though the program is not complete it works!

    Thank you, anduril462!

    If someone is curious though here is the working code:

    Code:
    /*
    learning.c
    05-18-2012
    Layvian
    -----------
    The purpose of this program is to store
    character values into memory using pointers.
    */
    #include <stdio.h>
    #include <stdbool.h>
    void lifeState(void);
    void learnStorage(int status, int *pStatus);
    void responseIn(int *pMemSlot1, int *pMemSlot2);
    void responseOut(int memSlot1, int memSlot2);
    int main(){
    lifeState();
    return 0;
    }
    /*
    Life State Function
    While this function is in the Loop the program
    will continiue to store char values in memory.
    Once ended the life state will end displaying
    the output of the two stored values and their
    address.
    */
    void lifeState(void){
    int status = 1;
    int *pStatus;
    pStatus = &status;
    do{
    learnStorage(status, pStatus);
    }while(status == 1);
    printf("End\n");
    }
    //Learn Storage Function
    void learnStorage(int status, int *pStatus){
    /*
    MEMORY SLOT 1
    Created with two types:
    1. Character
    2. Boolean
    Each has a pointer that references the originals
    address. Once the variable slot contains a char,
    it will be entered into the IF logic to switch the
    FALSE status of the "memSlot1Full", to TRUE.
    */
    int memSlot1;
    int *pMemSlot1;
    pMemSlot1 = &memSlot1;
    _Bool memSlot1Full = false;
    _Bool *pMemSlot1Full;
    pMemSlot1Full = &memSlot1Full;
    
    /*
    MEMORY SLOT 2
    Created with two types:
    1. Character
    2. Boolean
    Each has a pointer that references the originals
    address.  Once the variable slot contains a char,
    it will be entered into the IF logic to switch the
    FALSE status of the "memSlot2Full", to TRUE.
    */
    int memSlot2;
    int *pMemSlot2;
    pMemSlot2 = &memSlot2;
    _Bool memSlot2Full = false;
    _Bool *pMemSlot2Full;
    pMemSlot2Full = &memSlot2Full;
    responseIn(pMemSlot1, pMemSlot2);
    printf("%d\n", memSlot1);
    *pStatus = 0;
    }
    
    //Response Out Function
    //Response In Function
    void responseIn(int *pMemSlot1, int *pMemSlot2){
    int responseOne = 12;
    int responseTwo = 50;
    *pMemSlot1 = responseOne;
    *pMemSlot2 = responseTwo;
    }
    
    //Response Out Function
    void reponseOut(int memSlot1, int memSlot2){
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Please learn about indentation.
    Your code is unreadable.
    SourceForge.net: Indentation - cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Broken Box of Syntax
    Join Date
    May 2012
    Location
    Florida
    Posts
    19
    Quote Originally Posted by Salem View Post
    Please learn about indentation.
    Your code is unreadable.
    SourceForge.net: Indentation - cpwiki
    Truth be told I did indent, but when I transfered it to here it did not save the indention. I even tried pasting it onto notepad, but when I pasted it in here it did not save my spacing or indention.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It helps to use spaces instead of tabs, and copy-paste as plain text. May fancy editors and IDEs copy as rich text, which can bork the indentation and our built-in syntax highlighter. Also, as a courtesy to those helping you, preview your post and make sure it looks good before you submit. Fix any indentation by hand if you need to.

  9. #9
    Broken Box of Syntax
    Join Date
    May 2012
    Location
    Florida
    Posts
    19
    Quote Originally Posted by anduril462 View Post
    It helps to use spaces instead of tabs, and copy-paste as plain text. May fancy editors and IDEs copy as rich text, which can bork the indentation and our built-in syntax highlighter. Also, as a courtesy to those helping you, preview your post and make sure it looks good before you submit. Fix any indentation by hand if you need to.

    No offense was intended, thank you both for your help. I work in a call center where I wrote the above program and between online compiler and my post, I missed my indention.

    Sadly I know how important it is, considering python <--- My origin language I learned when I started programming requires indentation. Hence my confusion coming into C. Lol



    Edit: I did find a better Online Compiler then the one I was using: codepad.

    http://compilr.com

    It seems to work more efficiently and allows both tabing and spacing. Just for anyone who is interested.
    Last edited by Layvian; 05-18-2012 at 02:10 PM. Reason: Addition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A bit of an issue, converting char into int...
    By EssiJoon in forum C Programming
    Replies: 6
    Last Post: 05-17-2012, 02:03 PM
  2. Basic Char Array Issue
    By aussiemcgr in forum C Programming
    Replies: 5
    Last Post: 10-27-2010, 03:05 PM
  3. pointer issue
    By Justinb in forum C Programming
    Replies: 3
    Last Post: 10-17-2010, 08:26 PM
  4. Issue with scanf and char
    By Covalent in forum C Programming
    Replies: 6
    Last Post: 11-03-2008, 12:06 AM
  5. Need help with probably a very simple char sting issue
    By Prometheus in forum C++ Programming
    Replies: 14
    Last Post: 01-10-2007, 08:02 PM