I'm writing a code to put names in alphabetical order. The code i've written wont compile as its teling me that 'left operand needs l value'. The lines it refers to is where i'm trying to swap around strins using pointers. I could use some help with this, thanks!

#include<stdio.h>
#include<string.h>
main(void){

#define CLASS_SIZE 5
#define NAME_LEN   30

	char *string[CLASS_SIZE][NAME_LEN];
	int i=0;
	int j=0;
    char *temp;

	for(i=0;i<CLASS_SIZE;i++){
		printf("Enter a surname, first  name and age\n");
		gets(string[i]);
	}

	for(i=0;i<CLASS_SIZE;i++){
		puts(string[i]);
	}
     
	for(j=0;j<CLASS_SIZE;j++){

    for(i=0;i<CLASS_SIZE;i++){
		if(strncmp(string[i],string[i+1],30)){
	   
	    temp=string[i];
	    string[i]=string[i+1];
	    string[i+1]=temp;
		}
	}
	}
	return 0;
}

Recommended Answers

All 6 Replies

This wouldn't come about during compilation, but when it runs there will be a problem.

for(i=0; i<CLASS_SIZE; i++) {  // array size is 5 (CLASS_SIZE), variable i will equal 4 at some point ...  

  // when i is 4, string[4+1] is outside desired memory bounds, so compare result will be undesired.
  if(strncmp(string[i], string[i+1], 30)) {

    temp=string[i];
    string[i]=string[i+1];
    string[i+1]=temp;
	
  }

}

Cheers, didnt think of that, i just replaced the 'i+1' with '(i+1)%5', i think that should solve the problem i think? Any ideas on the string swapping? It might be the combination of pointers and arrays?

I would have gone with:

for(i = 0; i < (CLASS_SIZE - 1); i++) {

As for the strings, string[i]=string[i+1]; doesn't work because it's a multi dimensional array and your trying to assign outside a valid address.

Think of it as :

string[5]
  [30]
  [30]
  [30]
  [30]
  [30]

string[0][0] = 0x0A // Valid
string[0] = 0x0A // Invalid, there is no l-value to modify.

Change the declaration of string[][] to string[] and recompile.

At the minute you have an array of arrays holding char pointers, 30 * 5 = storage for 150 pointers, I don't think that was your intention.

Member Avatar for MonsieurPointer

In C, you can't copy strings (char arrays) using the assignment operator. What you are doing is re-assigning the places in memory.

You will need to do a bit of copying using loops and strcpy.

You're going to have to swap each string manually, character by character, if you're going to want this to work.

We aren't swapping actual strings here, we're swapping the pointers.

Example:

char * ap_string[2];
char * temp_ptr = NULL;

// Set The Strings Initially

// Memory Location: ap_string[0] = 0xAA -> 'Bob'
// Memory Location: ap_string[1] = 0XBB -> 'Dan' 

temp_ptr = ap_string[0];

// temp_ptr = 0xAA

ap_string[0] = ap_string[1];
ap_string[1] = temp_ptr;

// Memory Location: ap_string[0] = 0xBB -> 'Dan'
// Memory Location: ap_string[1] = 0XAA -> 'Bob'
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.