Thread: Modifying an element in an array

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    51

    Modifying an element in an array

    I've been trying to insert an element into an array with little success. I've been able to find similar examples but am still unable to get my code to work. So any help would be appreciated.

    The aim of the function is to check a phone number for a space as the fifth digit and insert one if it doesn't exist. The code below doesn't compile (error below). All I am trying to achieve at the moment is to change the 5th digit without dealing with shifting the other elements within the array.

    Code:
    /* Pass in the phone number, check there is a space in position 5 and insert if not present. */
    
    void ValidatePhoneNumber(char *phoneNumber)
    {
    
    	if(&phoneNumber[5] != " ")
    	{
    		&phoneNumber[5] = " ";
    	}
    	
    }
    Error message :
    In file included from driver.c:1:
    validation.h: In function ‘ValidatePhoneNumber’:
    validation.h:51: error: lvalue required as left operand of assignment


    Thanks in adance for any help / guidance

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Code:
    void ValidatePhoneNumber(char *phoneNumber)
    {
    
    	if (phoneNumber[4] != " ") {
    		phoneNumber[4] = " ";
    	}
    	
    }

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    Code:
    void ValidatePhoneNumber(char *phoneNumber)
    {
    
    	if (phoneNumber[4] != ' ') {
    		phoneNumber[4] = ' ';
    	}
    	
    }
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by Dino View Post
    Code:
    void ValidatePhoneNumber(char *phoneNumber)
    {
    
    	if (phoneNumber[4] != ' ') {
    		phoneNumber[4] = ' ';
    	}
    	
    }
    Right, I didn't notice it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Absolute value of each element in a given array
    By DriftinSW20 in forum C Programming
    Replies: 9
    Last Post: 11-15-2007, 04:08 PM
  2. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  3. Replies: 19
    Last Post: 07-20-2007, 01:46 AM
  4. Deleting an element from an array
    By Matt13 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 04:42 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM