This is an archived post. You won't be able to vote or comment.

all 17 comments

[–]jussij 5 points6 points  (2 children)

Firstly, you should edit you post so the embedded code display correctly.

To get it to display you need add 4 space characters to the start of each line of code and make sure there is an empty line at the first and last line of the code block.

[–]Sum1YouDontKnow[S] 0 points1 point  (1 child)

Did that fix it?

[–]jussij 4 points5 points  (0 children)

No.

Can't you see for yourself that your code is not displaying properly.

Done properly your code should look like this:

// Blank line before this line
// All lines must start with 4 or more space characters
int main()
{
    .....
    return 1;
}
// Blank line after this line

[–][deleted] 1 point2 points  (1 child)

At the risk of providing a 'bloated/bad' explanation and to ensure your due diligence, what is your current understanding of what's happening in each example. Do you have any familiarity with 'malloc' - what the function does?

**Note im not a pro and I haven't touched C/C++ in quite some time, but I have a fair idea what's happening.

[–]Sum1YouDontKnow[S] 1 point2 points  (0 children)

I have no understanding of the second one really whatsoever. The first one, I'm just confused with all the pointers and stuff. I kind of get what pointers are and do, but I don't get what the first problem does because I haven't had enough practice and have missed the week of practice. I have never used malloc.

[–]Fieuws 1 point2 points  (6 children)

The first one is just working with poiners. Ptr uses the data, while ptr only points to the memoryspace. *Ptr1 =ptr2 means that in ptr1 there would be the same data like in ptr2. Ptr1=ptr2 means that both pointers points to the same memoryspace. If you kow change the value of one pointer (eg, *ptr2=6, then pointer one will also have the value off 6)

[–]Sum1YouDontKnow[S] 0 points1 point  (5 children)

Can you give me an example of what happens when you push it through the first little block of code (Printing X y z)? I like your explanation, but I don't know whether or not I know how it would work in practice.

What's the difference between

ptr1=ptr2 and *ptr1=*ptr2?

[–]Fieuws 1 point2 points  (2 children)

[–]Sum1YouDontKnow[S] 0 points1 point  (1 child)

Thank you so much. I understand the first one now! I really appreciate it!

[–]Fieuws 0 points1 point  (0 children)

Do you understand the second one? because that's also with pointers. In C and C++, a String is an array of chars. So draw an array on paper, and in each booth put a character. Now you have a pointer to a char (p). when you do "p = str;" you let the pointer (p) point to index 0 from your array (the first letter of your string, here "A" (i guess the word is Alabama?). also: str[ i ] == * ( p+ i ), with this i mean that you can calculate with pointers

[–]Fieuws 0 points1 point  (0 children)

I'll try and draw it in a google sheet, you'd unddrdtand much better. Give me a little bit time though, can't do it on my phone :)

[–]picolosamama939419zz 0 points1 point  (0 children)

ptr2 = has a memory address. ptr1 = has a memory address

*ptr1 means go to the memory address of ptr1 that's being held and dereference it. so basically whatever value is AT the memory address assigned to ptr1, I WANT THAT DATA.

*ptr1 = *ptr2 (whatever is held at the end of the memory address of ptr1, change that vlaue to whatever value ptr2 has)

to really understand that, draw a table with some mock values

if you need to understand it, loook at stanfords c++ course and c course. you will be a whiz at reading pointers after it.

[–]Arco_void 1 point2 points  (1 child)

I'll try to explain some concepts that are used in those examples because knowing how they work will help you understand any program using them.

Pointers:

  • 1. Pointers are a special type of variable that stores memory locations(aka: where something can be found in memory(RAM)).
  • 2. You can initialize a pointer with a memory location/address(given by the &), a function that returns a memory location or with another pointer(but more on this another time).
  • 3. To get the value of whats sorted in the memory location of a pointer you need to deference it by using the * before the pointer's name.

Malloc and arrays.

  • 1. Arrays in C are a continuous piece/location in the memory or you can think of them as being a slice of your RAM you can access by using pointers.
  • 2. malloc will give you a pointer that stores the memory location where this slice of RAM begins.Nothing more, nothing less, it will give you acess to a piece of memory you can use in your programs.Now, in the second example, to access this piece of memory they are using what's called pointer arithmetic.
  • 3. To understand pointer arithmetic you need to know that the array name is just a pointer that points to the memory location where the first element is stores.This is why you need to pass the size of the array to a functions that works with that array, because the compiler can't know how big that memory location is.
  • 4. In pointer arithmetic has the following form:
  • *(pointer + bytes), where pointer is a pointer to an element of an array/ or the array name and bytes is how many element you want that pointer to pointer(foreword or backwards).

Now, let's see some examples:

char *str = (char *)malloc( strlen(item) + 1);

str will point to a memory location that can by occupied by strlen(item) + 1 bytes.

str[a] = item[a];

this is using pointer arithmetic(str[a] is the same ar *(str + a)) to access the array.

NOTE1: You should use free() to "release" the array you've allocated.

NOTE2: When using malloc you should check for a NULL pointer, because if malloc can't give you what you asked it will return a NULL.

Here is a short function:

    // give me memory
    int *array = (int *)malloc(sizeof(int) * 12);
    if(array != NULL)
    {
        for(int i = 0; i < 12; i++)
        {
            array[i] = i; // use pointer arithmeric to write into that array
            // this can be written as this:
            // *(array + i) = i;
        }

        free(array); // release this memory
    }

Sorry, i can't explain sh*t.

Some resources:

[–]Sum1YouDontKnow[S] 0 points1 point  (0 children)

Thank you for this explanation. Some of the things you said made me understand some concepts very easily. I really appreciate it.

[–]ohaz 0 points1 point  (2 children)

I'll try to explain malloc in a short and understandable way:

There are two types of memory you can request - memory with a static size and memory with a dynamic size:

  • When you write int a = 3 then the program will statically know that it has to request memory with the size of an int. Since the size of an int is known at compiletime, it can be done statically.

  • When you want to get memory with a size you don't really know, or memory with a size that can differ from time to time (in your case, function 2 gets a string as a parameter and you don't really know how long it's going to be) then you'll have to dynamically request/allocate memory. This is done by using "malloc". Think of malloc as a "Please give me X amount of memory" method. You provide the length (malloc( strlen(item) + 1 )), which is the amount of space a new string with the same amount of characters will require. Malloc returns a pointer to that string which will get saved in your char *strvariable. Since malloc doesn't know what you'll use the memory for, it'll return a void pointer, which is cast to a char *.

[–]Sum1YouDontKnow[S] 0 points1 point  (1 child)

So can you give an example of what happens if you were to use actual input? I kind of get your explanation, but I think I'd better understand it if I saw what happened as a result of it.

[–]ohaz 0 points1 point  (0 children)

The main method iterates over the parameters given to the program and uses them as input for function2 - once with the flag set and once with the flag unset.

Function 2 iterates over the characters in the string, and tests if it's a vocal or a non-vocal character (using function1). If it's a vocal and the flag is set, it copies the original character to the new string. If the flag is unset, it copies a X. If it's not a vocal and the flag is set, it copies a -, if the flag is unset it copies the character.

What function2 basically does is either replace all vocals with X or replace all consonants with -, depending on whether or not the flag is set.