CEMETECH
Leading The Way To The Future
Login [Register]
Username:
Password:
Autologin:

Don't have an account? Register now to chat, post, use our tools, and much more.
Latest Headlines
Online Users
There are 120 users online: 6 members, 84 guests and 30 bots.
Members: Ashbad, Eeems, flyingfisch, Piguy-3.14, ProgrammerNerd, roblabla.
Bots: VoilaBot (3), Magpie Crawler (3), VoilaBot (8), Yahoo! Slurp (1), Googlebot (15).
RSS & Social Media
SAX
You must log in to view the SAX chat widget
Author Message
yeongJIN_COOL


Member


Joined: 28 Apr 2011
Posts: 161

Posted: 18 Feb 2012 07:22:11 pm    Post subject: C questions.

I think I'll ask my c questions here.

First of all, how do I store string?
EDIT: not with scanf. I mean stuff like String something = "something";
even though I don't think this works in C.
Back to top
KermMartian


Site Admin


Joined: 14 Mar 2005
Posts: 55732
Location: Earth, Sol, Milky Way

Posted: 18 Feb 2012 07:25:29 pm    Post subject: Re: C questions.

yeongJIN_COOL wrote:
I think I'll ask my c questions here.

First of all, how do I store string?
In straight C, a string is an array of characters. If you define a constant string, it looks something like this:


Code:
  const char* somestring = "Hello World";


That string will take 12 bytes: eleven for the characters, and one byte for the null-terminator (a byte of zero or 0x00) that marks the end of the string. If you want a space to put your own string, you could do it like this:


Code:
  char stringspace[20];


Which would give you 20 bytes to work with for a 19-character string. Alternatively, you could use malloc:


Code:
  char* mystring; //notice it's a pointer, not an array
  if (NULL == (mystring = malloc(256))) {
    exit(-1); //failed to allocate memory!
  }
  //success, now we can use it


This will allocate 256 bytes for a string, or quit the program if the malloc (memory allocate) call fails.
_________________




Last edited by KermMartian on 18 Feb 2012 07:26:27 pm; edited 1 time in total
Back to top
seana11


Super-Expert


Joined: 23 May 2011
Posts: 833
Location: Well, the sign says "You are here"...

Posted: 18 Feb 2012 07:25:41 pm    Post subject: Re: C questions.

yeongJIN_COOL wrote:
I think I'll ask my c questions here.

First of all, how do I store string?
EDIT: not with scanf. I mean stuff like String something = "something";
even though I don't think this works in C.



Code:
char[] something= "something";

_________________

Code:
-----BEGIN GEEK CODE BLOCK-----
GCS d- s+: a---@ C+++ UL++ P L+++ E- W++ N o? K? w--- O? M--
V- PS++(--) PE- Y+ PGP t 5? X(+) R tv-- b++(+++) DI+(++)
D(+) G e-(*)>++@ h! r!>+++ y?
------END GEEK CODE BLOCK------
decoded
Back to top
yeongJIN_COOL


Member


Joined: 28 Apr 2011
Posts: 161

Posted: 18 Feb 2012 07:29:58 pm    Post subject:

so since strings are series of chars, this get one char? string[1]
Back to top
KermMartian


Site Admin


Joined: 14 Mar 2005
Posts: 55732
Location: Earth, Sol, Milky Way

Posted: 18 Feb 2012 07:31:02 pm    Post subject:

yeongJIN_COOL wrote:
so since strings are series of chars, this get one char? string[1]
Close! First, the term is "array", not "series;" let's be precise. Second, C arrays are indexed from zero. myarray[0] is the first element, myarray[1] is the second, and myarray[x] is the X+1th.

Edit: ah, you said "this [will] get one char[acter]," not "this [will] get the first char[acter]". My error. Smile
_________________


Back to top
seana11


Super-Expert


Joined: 23 May 2011
Posts: 833
Location: Well, the sign says "You are here"...

Posted: 18 Feb 2012 07:33:48 pm    Post subject:

yeongJIN_COOL wrote:
so since strings are series of chars, this get one char? string[1]


No.

Strings are arrays of chars. An array is just a pointer to a space in memory that has the next (however many units long)*unit-size bytes in length. And remember, arrays start at indice 0, because x[i] is equivlant to *(x + i).
_________________

Code:
-----BEGIN GEEK CODE BLOCK-----
GCS d- s+: a---@ C+++ UL++ P L+++ E- W++ N o? K? w--- O? M--
V- PS++(--) PE- Y+ PGP t 5? X(+) R tv-- b++(+++) DI+(++)
D(+) G e-(*)>++@ h! r!>+++ y?
------END GEEK CODE BLOCK------
decoded
Back to top
yeongJIN_COOL


Member


Joined: 28 Apr 2011
Posts: 161

Posted: 18 Feb 2012 07:40:21 pm    Post subject:

more question: is this possible?

Code:

string[1] = other_string[4];
Back to top
KermMartian


Site Admin


Joined: 14 Mar 2005
Posts: 55732
Location: Earth, Sol, Milky Way

Posted: 18 Feb 2012 07:41:56 pm    Post subject:

yeongJIN_COOL wrote:
more question: is this possible?

Code:

string[1] = other_string[4];
Yes. Since string[1] is a char, and other_string[4] is a char, you can do that. However, string = other_string; would not copy all the characters from other_string to string; instead, it would make string point to the same memory area as other_string. The strcpy() call (or a simple for loop) would serve to copy one string to the other.
_________________


Back to top
Tari


Systems Integrator


Joined: 03 Jul 2006
Posts: 2106
Location: Always-winter, Michigan

Posted: 18 Feb 2012 07:45:37 pm    Post subject:

KermMartian wrote:
However, string = other_string; would not copy all the characters from other_string to string; instead, it would make string point to the same memory area as other_string.

Note also that such a thing will fail to compile if they're both declared as arrays:

Code:

char foo[] = "Hello!";
char bar[16] = "";

// Will not compile
bar = foo;

// Copy foo ("Hello!") to bar
strncpy(bar, foo, sizeof(bar))

I see people trying to do the former enough that I wanted to head off the conclusion that it's legal.
_________________


Ask questions the smart way · タリ
Back to top
KermMartian


Site Admin


Joined: 14 Mar 2005
Posts: 55732
Location: Earth, Sol, Milky Way

Posted: 18 Feb 2012 07:49:39 pm    Post subject:

While we're being perfectly pedantic and correct about defensively programming, notice that Tari copied based on the size of the destination rather than the length of the source string, to make sure that it wouldn't overflow its destination container.
_________________


Back to top
yeongJIN_COOL


Member


Joined: 28 Apr 2011
Posts: 161

Posted: 19 Feb 2012 11:37:35 am    Post subject:

more question:
is it possible to create array that have undefined size and add in the elements one by one?
Back to top
Ashbad


... I think redheaded girls are kind of cool


Joined: 01 Dec 2010
Posts: 2417
Location: Stomp Stomp Stomp, The Idiot Convention

Posted: 19 Feb 2012 11:41:45 am    Post subject:

yeongJIN_COOL wrote:
more question:
is it possible to create array that have undefined size and add in the elements one by one?


Not really. You could potentially have a pointer to malloc()'d memory, and then realloc() each time you want to manually expand the size of the array, but that's pretty much the only option.
_________________
-Ashbad
Back to top
merthsoft


File Archiver


Joined: 09 May 2010
Posts: 2735

Posted: 19 Feb 2012 11:49:37 am    Post subject:

There're a few ways to go about doing this, depending on what you're wanting to do. You could just make a really big array bigger than you'll need. You could make a smaller array, and once you fill it make a new array twice the size and copy the contents into it. You could use a linked list. They've all got their pros and cons. How are you wanting to use it?
_________________
Shaun
Back to top
yeongJIN_COOL


Member


Joined: 28 Apr 2011
Posts: 161

Posted: 19 Feb 2012 12:20:17 pm    Post subject:

This is my unfinished code and it gives me error at char[] String= part. Why?

Code:


#include <stdio.h>
#include <string.h>
int main()
{
    int number_to_convert;
    int basein;
    int answer;
    int temp;
    printf("Type in the number");
    scanf("%d",&number_to_convert);
    printf("Type in the base you're converting to");
    scanf("%d",&basein);
    if (basein >36)
    return 0;
    int leftover;
    char[] String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    temp = number_to_convert;
    char Answer[100];
    int counter = 1;
    while (temp)
    {
       leftover = temp%basein;
       temp = temp/basein;


    }

}
Back to top
graphmastur


Power User


Joined: 27 Jul 2010
Posts: 464

Posted: 19 Feb 2012 12:21:15 pm    Post subject:

shouldn't it be char String[]?
Back to top
yeongJIN_COOL


Member


Joined: 28 Apr 2011
Posts: 161

Posted: 19 Feb 2012 12:22:38 pm    Post subject:

oh. thanks.
I guess there was a typo from the tutorial I was reading XD
Back to top
yeongJIN_COOL


Member


Joined: 28 Apr 2011
Posts: 161

Posted: 19 Feb 2012 12:36:40 pm    Post subject:

ok. here's the finished code I have.
However, the output is backwards and have weird character at front. What's wrong with my code?

input >> Decimal integer, base to convert
output << supposed to be converted number.


Code:

#include <stdio.h>
#include <string.h>
int main()
{
    int number_to_convert;
    int basein;
    int answer;
    int temp;
    printf("Type in the number");
    scanf("%d",&number_to_convert);
    printf("Type in the base you're converting to");
    scanf("%d",&basein);
    if (basein >36)
    return 0;
    int leftover;
    char String[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    temp = number_to_convert;
    char Answer[100];
    int counter = 1;
    while (temp)
    {
       leftover = temp%basein;
       temp = temp / basein;
       Answer[counter] = String[leftover];
       counter++;
    }
    printf(Answer);

}


EDIT: Never mind. I think I had a problem with math here. I'll look over it.
Back to top
Kllrnohj


/=\ PH34R |\/|3


Joined: 24 May 2005
Posts: 8189

Posted: 19 Feb 2012 07:59:45 pm    Post subject:

The first thing you need to do is pick a naming convention and *STICK WITH IT*

The amount that will help cannot be overstated.

Also if you are just storing a constant make the type "static const char*" (so in your case that would be String - which you really need to give a much better name)

As for your output, arrays are indexed from 0, not 1, and you need to terminate your string with a null character ('\0'). As for why it is backwards, that's because you are putting characters into your array backwards.
_________________
There are only two kinds of programming languages: those people always bitch about and those nobody uses. (Bjarne Stroustrup)
Back to top
Display posts from previous:   
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
    »
» View previous topic :: View next topic  
Page 1 of 1 » All times are GMT - 5 Hours

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© Copyright 2000-2013 Cemetech & Kerm Martian :: Page Execution Time: 0.041942 seconds.