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.
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.
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";
so since strings are series of chars, this get one char? string[1]
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
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).
more question: is this possible?

Code:

string[1] = other_string[4];
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.
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.
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.
more question:
is it possible to create array that have undefined size and add in the elements one by one?
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.
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?
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;


    }

}
shouldn't it be char String[]?
oh. thanks.
I guess there was a typo from the tutorial I was reading XD
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.
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.
  
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
Page 1 of 1
» All times are UTC - 5 Hours
 
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

 

Advertisement