LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 10-05-2007, 11:35 AM   #1
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Rep: Reputation: 30
What is wrong with this C code?


Can someone explain to me why the following C code is not working? Thanks

Code:
#include <stdio.h>
#include <stdlib.h>


int main ()
{


char string[] = "string";

char *var;

int i;



for(i=0; i<=6 ;i++){


int six = 6;

var = calloc(six, sizeof(char));

*(var + i) = string[i];


}


printf("%c", *(var + 0));
printf("%c", *(var + 1));
printf("%c", *(var + 2));
printf("%c", *(var + 3));
printf("%c", *(var + 4));
printf("%c", *(var + 5));


return 0;

}
 
Old 10-05-2007, 11:43 AM   #2
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,832

Rep: Reputation: 108Reputation: 108
Hya,

I am not sure what you want to program, however, if I guess you want to print out string, following is the story:

You allocate memory to *var six times, so that only the last memory space allocated is printed out.

Happy Penguins!
 
Old 10-05-2007, 11:45 AM   #3
swodniw
Member
 
Registered: Jan 2006
Posts: 35

Rep: Reputation: 16
It is working, just the way you want it to work is not correct

Think about it, if you allocate and assign in the first loop to index 0 then continue to the next loop and allocate and assign to index 1, what is the value at index 0?
 
Old 10-05-2007, 11:56 AM   #4
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
Thanks guys I get it now.
 
Old 10-05-2007, 12:02 PM   #5
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,832

Rep: Reputation: 108Reputation: 108
Hya,

index 0 is "zero" because memory is allocated by calloc.

Happy Penguins!
 
Old 10-05-2007, 12:10 PM   #6
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
You might also want to consider the termination test in your first for loop: You seem to be assigning seven values, but only using six of them.
 
Old 10-05-2007, 12:28 PM   #7
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
Quote:
Hya,

index 0 is "zero" because memory is allocated by calloc.

Happy Penguins!
Thanks


Quote:
You might also want to consider the termination test in your first for loop: You seem to be assigning seven values, but only using six of them.
I understand what you mean, but can you show me how you would do that?
 
Old 10-05-2007, 01:02 PM   #8
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
Is it what you want?

Code:
#include <stdio.h>
#include <stdlib.h>

int main ()
{
char *string = "string";
char *var, size;

size = strlen(string);

if (var = calloc(size, sizeof(char)))
{
   memcpy(var, string, size);

   while(size--)
   {
       printf("%c", *var++); 
   }
}
else
{
   printf("Cannot allocate memory\n");
}

return 0;
}
 
Old 10-05-2007, 01:29 PM   #9
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
I don't understand this part:

Code:
while(size--)
   {
       printf("%c", *var++); 
   }
Would you mind telling me what it does exactly?

Thanks
 
Old 10-05-2007, 04:24 PM   #10
Fidori
LQ Newbie
 
Registered: Oct 2007
Location: Finland
Distribution: Slackware
Posts: 27

Rep: Reputation: 17
The expression "variable++" increments the variable by one and returns the value that the variable had before incrementing. "variable--" works similarly but decrements the variable.
 
Old 10-05-2007, 04:39 PM   #11
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
Quote:
while(size--)
{
printf("%c", *var++);
}
var - is the pointer (e.g. address of a memory location)
*var - is content of a memory pointed by "var"
*var++ - read memory location at address "var" and increment value of "var" by 1.

Let say, the initial value of "var" is 100, then "*var++" will return value stored at location 100, and increments "var" by 1, so next time location at address 101 will be read.

There are few things to point out:
Quote:
char *string = "string"
will have terminating zero at the end, because it is declared as a string, but after your program execution "var" have only six bytes of "string" without terminating zero. In other words there is a difference between these declarations:
Quote:
char string[] = "string"; /* have zero at the end */
and
char string[] = {'s', 't', 'r', 'i', 'n', 'g'}; /* no zero at the end*/

Last edited by rsashok; 10-05-2007 at 04:42 PM.
 
Old 10-05-2007, 08:28 PM   #12
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
<snip>I understand what you mean, but can you show me how you would do that?</snip>
Sheesh! If you "understand" what I meant, why ask?

What I hope you understood, is that the "=" is not needed in this statement:

for(i=0; i<=6 ;i++){

<edit>
I don't think that the order of operation makes any difference in the increment of i, but, as rsasock pointed out, there is a difference between ++i and i++
</edit>

Last edited by PTrenholme; 10-05-2007 at 08:39 PM.
 
Old 10-06-2007, 11:03 AM   #13
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
Thanks guys, it is all clear to me now.



Quote:
<snip>I understand what you mean, but can you show me how you would do that?</snip>
Sheesh! If you "understand" what I meant, why ask?
I wasn't aware you meant the
Code:
i<=6
part when your where talking about the "termination test". I am trying to learn C, and you can't expect me to understand the language all at once.

Regards,

Ben
 
Old 10-11-2007, 01:09 AM   #14
kennithwang
Member
 
Registered: Aug 2007
Location: shanghai
Distribution: LDD
Posts: 32

Rep: Reputation: 15
#include <stdio.h>
#include <stdlib.h>


int main ()
{


char string[] = "string";

char *var;

//int i;
int j=0;



{


int six = 6;

var = (char *)calloc(six+1, sizeof(char));
for (j = 0; j < 6; j++)
*(var + j) = string[j];
*(var + 6) = '\0';

}


printf("%c\n", *(var + 0));
printf("%c\n", *(var + 1));
printf("%c\n", *(var + 2));
printf("%c\n", *(var + 3));
printf("%c\n", *(var + 4));
printf("%c\n", *(var + 5));


return 0;

}
 
Old 10-11-2007, 07:15 AM   #15
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
xie xie kennithwang

Tai Che
 
  


Reply



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
What wrong in this code kits Programming 10 01-10-2007 03:55 AM
anything wrong with this code? bytez Linux - Software 1 09-19-2006 04:52 AM
What is wrong with this C++ code? frankie_DJ Programming 5 10-01-2005 06:19 PM
What is wrong in this code hhegab Programming 12 08-13-2005 11:23 AM
What is wrong with this C code ? indian Programming 8 04-16-2005 04:42 AM

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

All times are GMT -5. The time now is 01:08 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