LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-03-2009, 02:13 AM   #1
j0hnsmith
LQ Newbie
 
Registered: Apr 2009
Posts: 23

Rep: Reputation: 0
C - assign new value to a string error


I'm learning C and have a problem that I can't work out.

You can declare an int and give it a new value any time you like but I can't do the same with a string. I want to change only the value of the string, not the size of string the variable can contain.

Code:
#include <stdio.h>

void main(void){ 
	
  int number; // declare
  number = 10; // assign value

  char test[100]; // declare

  printf("Please enter a word: ");
  scanf("%s", test);
	
  printf("sizeof test: %d\n", sizeof(test)); // still 100
  printf("%s\n", test);

  test = "fails"; // error - cannot convert from 'char [6]' to 'char [100]'. I don't want to change the size, only assign a new value (scanf is doing this).

}
 
Old 05-03-2009, 02:42 AM   #2
aspire1
Member
 
Registered: Dec 2008
Distribution: Ubuntu
Posts: 62

Rep: Reputation: 23
#include <string.h>

char *strcpy( char *s1, const char *s2)

*copies the string s2 into the character array s1. The value of s1 is returned.
 
Old 05-03-2009, 07:27 AM   #3
j0hnsmith
LQ Newbie
 
Registered: Apr 2009
Posts: 23

Original Poster
Rep: Reputation: 0
Thanks
 
Old 05-03-2009, 12:27 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by aspire1 View Post
#include <string.h>

char *strcpy( char *s1, const char *s2)

*copies the string s2 into the character array s1. The value of s1 is returned.
While that seems to solve j0hnsmiths problem, it still leaves him with the essential question unanswered.

In one place, he has declared an array of 100 char's, and called it 'test'. We are allowed to use the shorthand notation of referring to the address of the zeroth element of the array by simply using the variable name (the ultimate cause of this confusion). Nevertheless, the array is still 100 char's.
Later in the code, j0hnsmith tries to make an assignment to the array of 100 chars called test. In the assignment, he says 'make the address of the zeroth element of the 100 char's equal to the address of the literal string "fails"'. Since this cannot happen, the compiler bails.
This shorthand use of arrays by name probably causes more confusion than any other issue in the C language. It is a convenience to those who understand it, but a tripping point to those who do not.

--- rod
 
Old 05-03-2009, 11:28 PM   #5
j0hnsmith
LQ Newbie
 
Registered: Apr 2009
Posts: 23

Original Poster
Rep: Reputation: 0
Thanks theNbomr, your explanation is very useful although I did realise that something like this was happening.

C takes a lot more thought compared to PHP and JavaScript
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to assign a string to a structure variable in C? daYz Programming 10 07-06-2022 11:07 AM
assign converted string to variable bmaheni Linux - Newbie 2 12-28-2007 08:41 AM
"ERROR: UDP_OpenSocket: bind: Cannot assign requested address" error trebor0926 Red Hat 1 09-26-2006 10:21 AM
ping6 error "conect: cannot assign requested addres lamsteven Linux - Networking 0 06-30-2005 07:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration