小编Mih*_*aru的帖子

理解指向malloc和free的指针

指针在C中是一个非常棘手的事情.因为很多人很难理解它,所以为了更好地理解我写了下面的代码:

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

int main(int argc, char *argv[])
{
    int *p; // pointer -> will be dynamic allocated
    int *a; // array -> will be dynamic allocated

    // print before allocate memory (1)
    printf("&p: %p\tp: %p\t*p: %d\n", &p, p, *p);
    printf("&a: %p\ta: %p\t*a: %d\n", &a, a, *a);
    printf("\n");

    // allocate memory (2)
    p = (int *)malloc(sizeof(int));
    a = (int *)malloc(sizeof(int) * 10);

    // print after allocate, but before give a value to poinetrs (3)
    printf("&p: %p\tp: %p\t*p: %d\n", …
Run Code Online (Sandbox Code Playgroud)

c arrays malloc free pointers

6
推荐指数
1
解决办法
4023
查看次数

-L和-l命令在Makefile中不起作用

我有以下Makefile:

TOP = ../Bank/src
CC = gcc

CFLAGS = -g -Wall -std=c99
LDFLAGS = -L$(TOP)
LFLAGS = -lVirtualBank
INCLUDES = -I$(TOP)/VirtualBank/ 
LIBS = VirtualBank.a

BANK_SOURCES = $(TOP)/bank.c 
VirtualBank_SOURCES = $(TOP)/VirtualBank/bankServer.c $(TOP)/VirtualBank/dataBase.c $(TOP)/VirtualBank/account.c

BANK_OBJECTS = $(BANK_SOURCES:.c=.o)
VirtualBank_OBJECTS = $(VirtualBank_SOURCES:.c=.o)
TARGET = bank

all: VirtualBank.a $(TARGET)

VirtualBank.a:$(VirtualBank_OBJECTS)
    rm -f $@
    ar cq $@ $(VirtualBank_OBJECTS)
    mv *.a $(TOP)/

$(VirtualBank_OBJECTS):$(VirtualBank_SOURCES)
    $(CC) -c $(CFLAGS) $(INCLUDES) $(VirtualBank_SOURCES)
    mv *.o $(TOP)/VirtualBank/

$(TARGET):$(BANK_OBJECTS) 
    $(CC) $(CFLAGS) $(INCLUDES) $(BANK_OBJECTS) -o $(TARGET) $(LDFLAGS) $(LFLAGS)
    chmod +x $(TARGET)

$(BANK_OBJECTS):$(BANK_SOURCES)
    $(CC) $(CFLAGS) -c …
Run Code Online (Sandbox Code Playgroud)

c c++ gcc makefile

0
推荐指数
1
解决办法
92
查看次数

在C中格式化字符串

我想用C语言做类似的事情:

# This example is in Python language

var1 = 10
var2 = 45
var3 = 76

text = "Numbers are: %d, %d, %d." % (var1, var2, var3)
Run Code Online (Sandbox Code Playgroud)

这可能在C?我想说一个纯C的解决方案,而不是C++.谢谢.

编辑:

我不想直接打印字符串,我只想存储格式化的字符串.

c python arrays string string-formatting

-1
推荐指数
1
解决办法
94
查看次数

标签 统计

c ×3

arrays ×2

c++ ×1

free ×1

gcc ×1

makefile ×1

malloc ×1

pointers ×1

python ×1

string ×1

string-formatting ×1